In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use Python+PyQt5 to achieve a simple browser related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this article on how to use Python+PyQt5 to achieve a simple browser will have a harvest, let's take a look.
Experimental environment
Operating system: Linux Mint
Editor: vim
Programming language: python3
Dependency installation
Install PyQt5
Qt is a cross-platform C++ application development framework.
Sudo apt-get install python3-pyqt5
After the installation, enter the python command line interface to test whether the installation is correct.
Python3 > > import PyQt5
If there is no prompt after executing the command, the installation is successful.
Programming realization
Qt provides QtWebKit modules for developers. QtWebKit is an open source project.
WebKit's web content rendering engine, which allows you to transform Wanwei more quickly
Net is integrated into Qt application.
The browser has a window that can be used to display web pages.
Create a browser
Qt's program calls the exec_ () method to enter the event loop by creating an instance of the QApplication class.
Then the program listens for all kinds of events in a loop and puts them in the message queue, from the queue at the appropriate time.
Column for extraction processing.
... # create an application by creating an instance of the QApplication class app = QApplication (sys.argv) # run the application and listen for the event app.exec_ ()
We can create toolbars using QToolBar provided by Qt
... # add navigation bar navigation_bar = QToolBar ("Navigation") # set icon size navigation_bar.setIconSize (QSize (16,16)) # add navigation bar to window self.addToolBar (navigation_bar).
The QAction class provides an abstract user interface action
# add button reload_button = QAction (QIcon ("icons/renew.png"), "reload", self)
Bind action to the actual function
Reload_button.triggered.connect (self.browser.reload)
These action can be placed in the widget
Navigation_bar.addAction (reload_button)
There is a powerful widget class QWidgets in Qt, based on which many other widgets can be derived. For example, QLineEdit is a single-line text box. Use this as an address bar and add an address bar for browsing.
# add URL address bar self.urlbar = QLineEdit ()
Each component in Qt has a signal mechanism, which can be used to connect and bind the signal with the corresponding processing function. For example, the return signal urlbar.returnPressed of the address bar is bound with the navigate_to_url function. When the carriage return signal of the local address bar is sent, the function navigate_to_url will be triggered for processing.
# enable the address bar to respond to the enter key signal self.urlbar.returnPressed.connect (self.navigate_to_url) # navigate_to_url function def navigate_to_url (self): Q = QUrl (self.urlbar.text ()) if q.scheme () = "": q.setScheme ("http") self.browser.setUrl (Q)
Code
# v1. Noinspection PyUnresolvedReferences def created# by Roger# in 2017.1.3from PyQt5.QtCore import * from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtWebKitWidgets import * import sysclass MainWindow (QMainWindow): # noinspection PyUnresolvedReferences def _ _ init__ (self, * args, * * kwargs): super (). _ _ init__ (* args) * * kwargs) # set window title self.setWindowTitle ("My Browser") # set window icon self.setWindowIcon (QIcon ("icons/penguin.png")) # set window size 900 '600 self.resize (900 Self.show () # set browser self.browser = QWebView () url = "http://blog.csdn.net/roger_lzh" # specify the URL self.browser.setUrl of the open interface (QUrl (url)) # add a browser to the window self.setCentralWidget (self.browser) # create a navigation bar using QToolBar And use the QAction create button # add navigation bar navigation_bar = QToolBar ("Navigation") # set the size of the icon navigation_bar.setIconSize (QSize (16,16)) # add navigation bar to the window self.addToolBar (navigation_bar) # QAction class provides an abstract user interface action These action can be placed in the widget # add forward, rewind, stop loading and refresh buttons back_button = QAction (QIcon ("icons/back.png"), "Back", self) next_button = QAction (QIcon ("icons/next.png"), "Forward", self) stop_button = QAction (QIcon ("icons/cross.png"), "stop" Self) reload_button = QAction (QIcon ("icons/renew.png"), "reload" Self) back_button.triggered.connect (self.browser.back) next_button.triggered.connect (self.browser.forward) stop_button.triggered.connect (self.browser.stop) reload_button.triggered.connect (self.browser.reload) # add a button to the navigation bar navigation_bar.addAction (back_button) navigation_bar.addAction (next_button) Navigation_bar.addAction (stop_button) navigation_bar.addAction (reload_button) # add URL address bar self.urlbar = QLineEdit () # so that the address bar can respond to the enter key signal self.urlbar.returnPressed.connect (self.navigate_to_url) navigation_bar.addSeparator () navigation_bar.addWidget (self.urlbar) # Let browse Self.browser.urlChanged.connect (self.renew_urlbar) def navigate_to_url (self): Q = QUrl (self.urlbar.text ()) if q.scheme () = "": q.setScheme ("http") self.browser.setUrl (Q) def renew_urlbar (self) Q): # Update the link to the current web page to the address bar self.urlbar.setText (q.toString ()) self.urlbar.setCursorPosition (0) # create the application app = QApplication (sys.argv) # create the main window window = MainWindow () # display window window.show () # run the application And listen to the event app.exec_ () on "how to use Python+PyQt5 to implement a simple browser" this article is introduced here, thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use Python+PyQt5 to achieve a simple browser". If you want to learn more, you are welcome to 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.