欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用pyQT5显示网页的实现步骤

程序员文章站 2022-06-24 21:17:43
目录1、pyqtwebengine安装库开发环境配置请参考博文:【python】在pycharm中使用pyqt5进行gui开发的基本流程1、pyqtwebengine安装库加载网页需要使用pyqtwe...

开发环境配置请参考博文:

【python】在pycharm中使用pyqt5进行gui开发的基本流程

1、pyqtwebengine安装库

加载网页需要使用pyqtwebengine库,可以使用import pyqt5.qtwebenginewidgets测试是否有这个库,如果没有,需要先安装

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

2、设计窗体

在qt designer里面,没有webengine窗体,只能在程序中创建,先将其他的部分放好。
这儿放置了一个lineedit,用于输入网址,一个按钮

使用pyQT5显示网页的实现步骤

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显示网页的实现步骤的文章就介绍到这了,更多相关pyqt5显示网页内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: pyQT5 显示网页