catalogue
- 1. The first is the installation of the environment (I use pycharm, python 3.6)
- 2. Implementation code
- 3. Operation results
- 4. Tips
1. The first is the installation of the environment (I use pycharm, python 3.6)
pip3 install PyQt5
(if no version is specified, the latest version will be installed by default)
pip3 install sip
pip3 install PyQtWebEngine
(pyqtwebengine is a set of frameworks of QT webengine, which provides the ability to embed web content in applications and is based on Chrome browser.)
2. Implementation code
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
import os
import sys
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.resize(2000, 1314)
self.show()
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)
self.webview = WebEngineView(self)
self.webview.load(QUrl("https://www.baidu.com"))
self.create_tab(self.webview)
navtb = QToolBar("Navigation")
navtb.setIconSize(QSize(16, 16))
self.addToolBar(navtb)
back_ BTN = qaction (qicon (OS. Path. Join ('images', 'houtui. PNG'), "back", self)
back_btn.triggered.connect(self.webview.back)
navtb.addAction(back_btn)
next_ BTN = qaction (qicon (OS. Path. Join ('images', 'Qianjin. PNG'), "forward", self)
next_btn.triggered.connect(self.webview.forward)
navtb.addAction(next_btn)
reload_ BTN = qaction (qicon (OS. Path. Join ('images', 'refresh_icon. PNG'), "Refresh", self)
reload_btn.triggered.connect(self.webview.reload)
navtb.addAction(reload_btn)
home_ BTN = qaction (qicon (OS. Path. Join ('images', 'geren1. PNG'), "home page", self)
navtb.addAction(home_btn)
self.urlbar = QLineEdit()
stop_btn = QAction(QIcon(os.path.join('images', 'cross-circle.png')), "Stop", self)
stop_btn.triggered.connect(lambda: self.tabs.currentWidget().stop())
navtb.addAction(stop_btn)
self.urlbar.returnPressed.connect(self.navigate_to_url)
navtb.addSeparator()
navtb.addWidget(self.urlbar)
self.webview.urlChanged.connect(self.renew_urlbar)
def navigate_to_url(self):
q = QUrl(self.urlbar.text())
if q.scheme() == '':
q.setScheme('http')
self.webview.setUrl(q)
def renew_urlbar(self, q):
self.urlbar.setText(q.toString())
self.urlbar.setCursorPosition(0)
def create_tab(self, webview):
self.tab = QWidget()
self. tabWidget. Addtab (self.tab, "new tab")
self.tabWidget.setCurrentWidget(self.tab)
self.Layout = QHBoxLayout(self.tab)
self.Layout.setContentsMargins(0, 0, 0, 0)
self.Layout.addWidget(webview)
def close_Tab(self, index):
if self.tabWidget.count() > 1:
self.tabWidget.removeTab(index)
else:
self.close()
class WebEngineView(QWebEngineView):
def __init__(self, mainwindow, parent=None):
super(WebEngineView, self).__init__(parent)
self.mainwindow = mainwindow
def createWindow(self, QWebEnginePage_WebWindowType):
new_webview = WebEngineView(self.mainwindow)
self.mainwindow.create_tab(new_webview)
return new_webview
app = QApplication(sys.argv)
browser = MainWindow()
browser.show()
sys.exit(app.exec_())
3. Operation results
4. Tips
If you are prompted which modules are missing, pychar will generally prompt you to install them. If you are not prompted, you can refer to the library I ran successfully:
Vector map, you can go online to download your favorite style, there are many code parts that can be downloaded for free, and refer to the article of brother crawling all over the world https://www.jb51.net/article/185526.htm
This is the end of this article about the sample code of Python pyqt5 module implementing a browser. For more information about pyqt5 browser, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!