pyqt5 QWebEngineView 使用多线程刷新本地加载的网页

制作程序时,为了能够实时的更新相应的温度数据(每隔10s)主程序代码

#程序的主线程
class Window_fjg_information(QWidget):
    def __init__(self):
        super(Window_fjg_information,self).__init__()
        self.setupUi()
    def setupUi(self):
        self.setObjectName("self")
        self.resize(1200, 800)
        self.fram_5_information_temp_graph()
        QtCore.QMetaObject.connectSlotsByName(self)
    def fram_5_information_temp_graph(self):
        self.web_show_temp_graph = QWebEngineView(self)#创建web浏览器模块
        self.refresh_temp = Refresh_temp_url() #建立子线程链接
        self.refresh_temp.single_refresh_temp_url.connect(self.refresh_temp_url_2)#链接信号并将数据创给方法refresh_temp_url_2
        self.refresh_temp.start()
    def refresh_temp_url_2(self,path_url):
        self.web_show_temp_graph.load(QUrl.fromLocalFile(path_url))#获得子程序传来的数据,每10s更新一次显示

子程序代码

class Refresh_temp_url(QThread):
    single_refresh_temp_url = pyqtSignal(str)
    def __init__(self):
        super(Refresh_temp_url,self).__init__()
    def run(self):
        while True:
            a = os.path.abspath(os.path.dirname(os.getcwd()))  # 获取上层目录
            self.id_fa = "QJ_01_01"
            self.url_path = str(a) + "/data_db/data_url_temp/{}.html".format(self.id_fa)
            self.single_refresh_temp_url.emit(str(self.url_path ))
            time.sleep(10)

此处使用QThread,加载多线程的方法,使用while 循环,每次返回QWebEngineView读取url的文件地址


from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import QWebEngineView
import sqlite3,json,os,time,threading
#加载url文件相对位置的子线程
class Refresh_temp_url(QThread):
    single_refresh_temp_url = pyqtSignal(str)
    def __init__(self):
        super(Refresh_temp_url,self).__init__()
    def run(self):
        while True:
            a = os.path.abspath(os.path.dirname(os.getcwd()))  # 获取上层目录
            self.id_fa = "QJ_01_01"
            self.url_path = str(a) + "/data_db/data_url_temp/{}.html".format(self.id_fa)
            self.single_refresh_temp_url.emit(str(self.url_path ))
            time.sleep(10)
#程序的主线程
class Window_fjg_information(QWidget):
    def __init__(self):
        super(Window_fjg_information,self).__init__()
        self.setupUi()
    def setupUi(self):
        self.setObjectName("self")
        self.resize(1200, 800)
        self.fram_5_information_temp_graph()
        QtCore.QMetaObject.connectSlotsByName(self)
    def fram_5_information_temp_graph(self):
        self.web_show_temp_graph = QWebEngineView(self)#创建web浏览器模块
        self.refresh_temp = Refresh_temp_url() #建立子线程链接
        self.refresh_temp.single_refresh_temp_url.connect(self.refresh_temp_url_2)#链接信号并将数据创给方法refresh_temp_url_2
        self.refresh_temp.start()
    def refresh_temp_url_2(self,path_url):
        self.web_show_temp_graph.load(QUrl.fromLocalFile(path_url))#获得子程序传来的数据,每10s更新一次显示
if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    fjg_gl = Window_fjg_information()
    fjg_gl.show()
    sys.exit(app.exec_())
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值