制作程序时,为了能够实时的更新相应的温度数据(每隔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_())