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 pyQT5 to display web pages

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to use pyQT5 to display web pages, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. PyQTWebEngine installation Library

You need to use the PyQTWebEngine library to load the web page. You can use import PyQt5.QtWebEngineWidgets to test whether this library exists. If not, you need to install it first.

Pip install PyQtWebEngine-I https://pypi.douban.com/simple/2, Design form

In QT Designer, there is no WebEngine form, you can only create it in the program, put the rest of it away first.

A LineEdit is placed here to enter the URL and a button

3. Generate code

Using pyUIC to generate code, the generated code is:

#-*-coding: utf-8-*-# Form implementation generated from reading ui file 'web.ui'## Created by: PyQt5 UI code generator 5.15.4 WARNING: Any manual changes made to this file will be lost when pyuic5 is# run again. Do not edit this file unless you know what you are doing.from PyQt5 import QtCore, QtGui, QtWidgetsclass Ui_MainWindow (object): def setupUi (self, MainWindow): MainWindow.setObjectName ("MainWindow") MainWindow.resize (1200) self.centralwidget = QtWidgets.QWidget (MainWindow) self.centralwidget.setObjectName ("centralwidget") self.lineEdit = QtWidgets.QLineEdit (self.centralwidget) self.lineEdit.setGeometry (QtCore.QRect (20,10,631) 31) self.lineEdit.setObjectName ("lineEdit") self.pushButton = QtWidgets.QPushButton (self.centralwidget) self.pushButton.setGeometry (QtCore.QRect (690,10,90,30) self.pushButton.setObjectName ("pushButton") MainWindow.setCentralWidget (self.centralwidget) self.menubar = QtWidgets.QMenuBar (MainWindow) self.menubar.setGeometry (QtCore.QRect (0,0,1200) 23) self.menubar.setObjectName ("menubar") MainWindow.setMenuBar (self.menubar) self.statusbar = QtWidgets.QStatusBar (MainWindow) self.statusbar.setObjectName ("statusbar") MainWindow.setStatusBar (self.statusbar) self.retranslateUi (MainWindow) QtCore.QMetaObject.connectSlotsByName (MainWindow) def retranslateUi (self, MainWindow): _ translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle (_ translate ("MainWindow") "MainWindow") self.lineEdit.setText (_ translate ("MainWindow", "http://www.baidu.com")) self.pushButton.setText (_ translate (" MainWindow "," Open ")) 4, add code

Import library files

From PyQt5 import QtCore, QtGui, QtWidgets,QtWebEngineWidgets

1. Add WebEngine forms

# # create web form self.qwebengine = QtWebEngineWidgets.QWebEngineView (MainWindow) self.qwebengine.setGeometry (20,50,600)

2. Create button function

# # creating a connection self.pushButton.clicked.connect (self.open_url)

3. Write button function

Def open_url (self): url=self.lineEdit.text () self.qwebengine.load (QtCore.QUrl (url))

4. Add a startup program

Import sysif _ _ name__ ='_ main__': app = QtWidgets.QApplication (sys.argv) MainWindow = QtWidgets.QMainWindow () # create a form object ui = Ui_MainWindow () # create a form object designed by PyQt ui.setupUi (MainWindow) # call the form method to initialize the object MainWindow.show () # display form sys.exit (app.exec_ ()) # exit the process when the program closes

5. Complete code

#-*-coding: utf-8-*-# Form implementation generated from reading ui file 'web.ui'## Created by: PyQt5 UI code generator 5.15.4 WARNING: Any manual changes made to this file will be lost when pyuic5 is# run again. Do not edit this file unless you know what you are doing.from PyQt5 import QtCore, QtGui, QtWidgets,QtWebEngineWidgetsclass Ui_MainWindow (object): def setupUi (self, MainWindow): MainWindow.setObjectName ("Open Web Page") MainWindow.resize (1640) self.centralwidget = QtWidgets.QWidget (MainWindow) self.centralwidget.setObjectName ("centralwidget") self.lineEdit = QtWidgets.QLineEdit (self.centralwidget) self.lineEdit.setGeometry (QtCore.QRect (20,10,630) 30) self.lineEdit.setObjectName ("lineEdit") self.pushButton = QtWidgets.QPushButton (self.centralwidget) self.pushButton.setGeometry (QtCore.QRect (690,10,90,30) self.pushButton.setObjectName ("pushButton") MainWindow.setCentralWidget (self.centralwidget) self.menubar = QtWidgets.QMenuBar (MainWindow) self.menubar.setGeometry (QtCore.QRect (0,0,1200) 23) self.menubar.setObjectName ("menubar") MainWindow.setMenuBar (self.menubar) self.statusbar = QtWidgets.QStatusBar (MainWindow) self.statusbar.setObjectName ("statusbar") MainWindow.setStatusBar (self.statusbar) self.retranslateUi (MainWindow) QtCore.QMetaObject.connectSlotsByName (MainWindow) # # create web forms self.qwebengine = QtWebEngineWidgets.QWebEngineView (MainWindow) self.qwebengine.setGeometry (20,50 1600) # # create a connection self.pushButton.clicked.connect (self.open_url) def retranslateUi (self, MainWindow): _ translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle (_ translate ("MainWindow", "Open Web Page")) self.lineEdit.setText (_ translate ("MainWindow", "http://www.baidu.com")) self.pushButton.setText (_ translate (" MainWindow ")) "Open") def open_url (self): url=self.lineEdit.text () self.qwebengine.load (QtCore.QUrl (url)) import sysif _ _ name__ = ='_ main__': app = QtWidgets.QApplication (sys.argv) MainWindow = QtWidgets.QMainWindow () # create form object ui = Ui_MainWindow () # create form object ui.setupUi (MainWindow) designed by PyQt ) # call the method of the form to initialize the object MainWindow.show () # display the form sys.exit (app.exec_ ()) # exit the process when the program closes

6. Run

The above is all the contents of the article "how to use pyQT5 to display web pages". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.

Share To

Development

Wechat

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

12
Report