使用pyQT5顯示網頁的實現步驟

開發環境配置請參考博文:

【python】在PyCharm中使用pyQT5進行GUI開發的基本流程

1、PyQTWebEngine安裝庫

加載網頁需要使用PyQTWebEngine庫,可以使用import PyQt5.QtWebEngineWidgets測試是否有這個庫,如果沒有,需要先安裝

pip install PyQtWebEngine -i https://pypi.douban.com/simple/

2、設計窗體

在QT Designer裡面,沒有WebEngine窗體,隻能在程序中創建,先將其他的部分放好。
這兒放置瞭一個LineEdit,用於輸入網址,一個按鈕

在這裡插入圖片描述

3、生成代碼

使用pyUIC生成代碼,生成的代碼為:

# -*- 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


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1200, 700)
        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", "打開"))

4、添加代碼

引入庫文件

from PyQt5 import QtCore, QtGui, QtWidgets,QtWebEngineWidgets

1、添加WebEngine窗體

        ## 創建web窗體
        self.qwebengine = QtWebEngineWidgets.QWebEngineView(MainWindow)
        self.qwebengine.setGeometry(20, 50, 1600, 600)

2、創建按鈕功能

        ## 創建連接
        self.pushButton.clicked.connect(self.open_url)

3、編寫按鈕函數

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

4、添加啟動程序

import sys

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()  # 創建窗體對象
    ui = Ui_MainWindow()  # 創建PyQt設計的窗體對象
    ui.setupUi(MainWindow)  # 調用窗體的方法對對象進行初始化設置
    MainWindow.show()  # 顯示窗體
    sys.exit(app.exec_())  # 程序關閉時退出進程

5、完整代碼

# -*- 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,QtWebEngineWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("打開網頁")
        MainWindow.resize(1640, 700)
        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)

        ## 創建web窗體
        self.qwebengine = QtWebEngineWidgets.QWebEngineView(MainWindow)
        self.qwebengine.setGeometry(20, 50, 1600, 600)

        ## 創建連接
        self.pushButton.clicked.connect(self.open_url)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "打開網頁"))
        self.lineEdit.setText(_translate("MainWindow", "http://www.baidu.com"))
        self.pushButton.setText(_translate("MainWindow", "打開"))


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

import sys

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()  # 創建窗體對象
    ui = Ui_MainWindow()  # 創建PyQt設計的窗體對象
    ui.setupUi(MainWindow)  # 調用窗體的方法對對象進行初始化設置
    MainWindow.show()  # 顯示窗體
    sys.exit(app.exec_())  # 程序關閉時退出進程

6、運行

在這裡插入圖片描述

到此這篇關於使用pyQT5顯示網頁的實現步驟的文章就介紹到這瞭,更多相關pyQT5顯示網頁內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: