当我使用线程时,PyQt5中没有更新GUI

时间:2019-03-08 16:40:01

标签: python multithreading user-interface pyqt5

我从带有Modbus Lib的控制器上读取了有关温度和电压的信息。当我以“尝试...除外”方式进行操作时,一切正常。 但是,当我使用线程在while循环中执行此操作时,GUI会保持恒定约20秒。 但是,温度打印工作正常。 然后,当我单击GUI上的按钮时,GUI信息就会更新。

from PyQt5 import QtCore, QtGui, QtWidgets, uic
from PyQt5.QtWidgets import QApplication
import time
from modbusFunction import *

使用Try的第一个代码除外,使用While循环和线程的第二个代码:

class test(QtWidgets.QMainWindow):
    def __init__(self):
        super(test, self).__init__()
        uic.loadUi('ui/test.ui', self)
        self.readTemp()

    def readTemp(self):
        try:
            temp = modbusFunction.modbusReadRegister(self, '192.168.1.13', 502, 0x1192, 1)
            print(temp[0])
            self.supplyTempResualt.setText(str(temp[0]))
        except Exception as err:
            print(err)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = test()
    window.show()
    sys.exit(app.exec_())

第二个代码:

from threading import Thread

class test(QtWidgets.QMainWindow):
    def __init__(self):
        super(test, self).__init__()
        uic.loadUi('ui/test.ui', self)

        t = Thread(target = self.readTemp)
        t.daemon = True
        t.start()

    def readTemp(self):
        while True:
            try:
                temp = modbusFunction.modbusReadRegister(self, '192.168.1.13', 502, 0x1192, 1)
                print(temp[0])
                self.supplyTempResualt.setText(str(temp[0]))
            except Exception as err:
                print(err)
            time.sleep(1)


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = test()
    window.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

您不应该直接从另一个线程更新GUI,要更新有以下方法:

1。 pyqtSignal()

with open('carstest.txt', 'r') as file :
    testdata = file.read()

colors = ['RED','GREEN','BLUE']
car =['Ferrari', 'Mustang','Camero']
Money = ['one','two','three']

i = 0
while i < len(colors):
    s = testdata
    s = s.replace('*color*' , colors[i])
    s = s.replace('*car*'   , car[i])
    s = s.replace('*Money*' , Money[i])
    print(s)
    i += 1
else:
    print ("DONE")

2。 QMetaObject :: invokeMethod

class test(QtWidgets.QMainWindow):
    textChanged = QtCore.pyqtSignal()

    def __init__(self):
        super(test, self).__init__()
        uic.loadUi('ui/test.ui', self)
        self.textChanged.connect(self.supplyTempResualt.setText)

        t = Thread(target = self.readTemp)
        t.daemon = True
        t.start()

    def readTemp(self):
        while True:
            try:
                temp = modbusFunction.modbusReadRegister(self, '192.168.1.13', 502, 0x1192, 1)
                print(temp[0])
                self.textChanged.emit(str(temp[0]))
            except Exception as err:
                print(err)
            time.sleep(1)

3。使用QTimer.singleShot()的functools.partial

def readTemp(self):
    while True:
        try:
            temp = modbusFunction.modbusReadRegister(self, '192.168.1.13', 502, 0x1192, 1)
            print(temp[0])
            QtCore.QMetaObject.invokeMethod(self.supplyTempResualt, "setText",
                QtCore.Qt.QueuedConnection, QtCore.Q_ARG(str, str(temp[0])))
        except Exception as err:
            print(err)
        time.sleep(1)