Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python PyQt5 module to implement a browser

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "how to use the Python PyQt5 module to achieve a browser", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use the Python PyQt5 module to achieve a browser" article.

1. The first is the installation of the environment (I use PyCharm,python3.6)

Pip3 install PyQt5 (if no version is specified, the latest one will be installed by default)

Pip3 install sip

Pip3 install PyQtWebEngine

PyQtWebEngine is a set of frameworks for QtWebEngine that provide the ability to embed Web content in applications and are based on Chrome browsers. )

two。 Implementation code from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtWebEngineWidgets import * import osimport sysclass 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 ("http://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 (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 ("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_webviewapp = QApplication (sys.argv) browser = MainWindow () browser.show () sys.exit (app.exec_ ()) 3. Running result

4. Tips

If you are prompted about which modules are missing, Pycharm will usually be prompted to install them. If you are not prompted, you can refer to the library I ran successfully:

The above is about the content of this article on "how to implement a browser with Python PyQt5 module". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report