库安装:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyqt5==5.13.2
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple PyQt5-tools
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple sip
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple PyQtWebEngine
源码:
1 import sys,os 2 os.chdir(os.path.dirname(os.path.abspath(__file__))) 3 from PyQt5.QtGui import * 4 from PyQt5.QtCore import * 5 from PyQt5.QtWidgets import * 6 from PyQt5.QtWebEngineWidgets import * 7 8 class MainWindow(QMainWindow): 9 def __init__(self, *args, **kwargs): 10 super().__init__(*args, **kwargs) 11 self.setWindowTitle('哆啦A梦浏览器') 12 #self.setWindowIcon(QIcon('icons/favicon.ico')) 13 self.resize(1000, 600) 14 self.show() 15 16 url = "网址!!!!!!!!!!!" 17 self.browser = QWebEngineView() 18 self.browser.load(QUrl(url)) 19 self.setCentralWidget(self.browser) 20 21 if __name__=='__main__': 22 app = QApplication(sys.argv) 23 window = MainWindow() 24 window.show() 25 sys.exit(app.exec_())
如果显示网络连接错误,或一片空白请用以下版本:
1 import sys 2 from PyQt5.QtCore import QUrl 3 from PyQt5.QtWidgets import QApplication 4 from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineView 5 app = QApplication(sys.argv) 6 browser = QWebEngineView() 7 browser.load(QUrl("网站!!!!!")) 8 browser.show() 9 app.exec_()
有标签版本:
import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtWebEngineWidgets import QWebEngineView ################################################ #######创建主窗口 ################################################ class MainWindow(QMainWindow): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle('My Browser') self.showMaximized() self.setWindowFlags(Qt.FramelessWindowHint) #####创建tabwidget self.tabWidget = QTabWidget() self.tabWidget.setTabShape(QTabWidget.Triangular) self.tabWidget.setDocumentMode(True) self.tabWidget.setMovable(True) self.tabWidget.setTabsClosable(True) self.tabWidget.tabCloseRequested.connect(self.close_Tab) self.setCentralWidget(self.tabWidget) ####第一个tab self.webview = WebEngineView(self) #self必须要有,是将主窗口作为参数,传给浏览器 self.webview.load(QUrl("http://www.baidu.com")) self.create_tab(self.webview) #创建tab def create_tab(self,webview): self.tab = QWidget() self.tabWidget.addTab(self.tab, "新标签页") self.tabWidget.setCurrentWidget(self.tab) ##### self.Layout = QHBoxLayout(self.tab) self.Layout.setContentsMargins(0, 0, 0, 0) self.Layout.addWidget(webview) #关闭tab def close_Tab(self,index): if self.tabWidget.count()>1: self.tabWidget.removeTab(index) else: self.close() # 当只有1个tab时,关闭主窗口 ################################################ #######创建浏览器 ################################################ class WebEngineView(QWebEngineView): def __init__(self,mainwindow,parent=None): super(WebEngineView, self).__init__(parent) self.mainwindow = mainwindow # 重写createwindow() def createWindow(self, QWebEnginePage_WebWindowType): new_webview = WebEngineView(self.mainwindow) self.mainwindow.create_tab(new_webview) return new_webview ################################################ #######程序入门 ################################################ if __name__ == "__main__": app = QApplication(sys.argv) the_mainwindow = MainWindow() the_mainwindow.show() sys.exit(app.exec_())