验证中...
1:整体代码
Raw Copy
# -*- coding: utf-8 -*-
# 导入所需模块
import os
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
class Browser_Encapsulation(QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# 设置浏览器窗口标题
self.setWindowTitle(r"Title")
# 设置窗口图标
self.absolute_path = os.path.abspath(os.path.join(os.getcwd(), "../.."))
self.browser = self.absolute_path + r"\Start_Init\Logo.png"
self.setWindowIcon(QIcon(self.browser))
# 设置窗口大小1000×600
self.resize(1000, 600)
self.show()
# 设置浏览器打开界面的URL
self.browser = QWebEngineView()
url = r"要打开的Url"
self.browser.setUrl(QUrl(url))
self.setCentralWidget(self.browser)
# 添加导航栏
navigation_bar = QToolBar('Navigation')
# 设定图标的大小
navigation_bar.setIconSize(QSize(1, 1))
#添加导航栏到窗口中
self.addToolBar(navigation_bar)
# 添加URL地址栏
self.urlbar = QLineEdit()
# 让地址栏能响应回车按键信号
self.urlbar.returnPressed.connect(self.navigate_to_url)
navigation_bar.addSeparator()
navigation_bar.addWidget(self.urlbar)
# 让浏览器相应url地址的变化
self.browser.urlChanged.connect(self.renew_urlbar)
# 隐藏导航栏,删除以下代码就可以显示导航栏
navigation_bar.setHidden(True)
# 打开URL
def navigate_to_url(self):
q = QUrl(self.urlbar.text())
if q.scheme() == '':
q.setScheme('http')
self.browser.setUrl(q)
# 更新URL
def renew_urlbar(self, q):
self.urlbar.setText(q.toString())
self.urlbar.setCursorPosition(0)
# 创建应用
app = QApplication(sys.argv)
# 创建主窗口
window = Browser_Encapsulation()
# 显示窗口
window.show()
# 运行应用,并监听事件
app.exec_()
2:运行效果

Comment list( 0 )

Sign in for post a comment

Help Search