每5秒PyQt更新一次文本框

时间:2015-10-02 17:31:22

标签: python pyqt pyqt4 qtgui qtcore

所以这是我的问题,我每隔5秒就会读取一条串行电缆的数据并存储在CSV文件中。我也把这些数据放到一个列表中。我想做的是获取变量5,7和9,并将它们显示在我的GUI中,我有Qtextboxes ...我该怎么做?

变量列表将在一个名为listvalues的值中。我想调用5,7和9,并将它们显示在PyQt窗口的各自文本框中。

这是我的代码:

from PyQt4 import QtGui
import sys
import masimo
import csv
import time
import datetime as DT
import threading
from threading import Thread
import serial
import os

os.chdir(r"C:\Users\SpO2\Desktop\Data")
time = time.strftime("%d %b %Y %H%M%S")
location = r'%s.csv' % time
outputfile = open(location, mode='x', newline='')
outputWriter = csv.writer(outputfile)
outputWriter.writerow(["start"])
outputfile.close()
port = "COM4"


class ExampleApp(QtGui.QMainWindow, masimo.Ui_MainWindow):
    def __init__(self, parent=None):
        super(self.__class__, self).__init__()
        self.setupUi(self)

def SerialRead():
    delay1 = DT.datetime.now()                
    ser = serial.Serial(port, baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)   
    out = ser.read(167)
    reading = str(out)
    plaintext1 = reading.replace(' ', ', ')
    plaintext = plaintext1.replace('=', ', ')
    listvalue = plaintext.split(", ")
    ser.close()

    outputfile = open(location, mode='a', newline='')
    outputWriter = csv.writer(outputfile)
    outputWriter.writerow([plaintext])
    outputfile.close()

    delay2 = DT.datetime.now()
    differencetime = (delay2 - delay1).total_seconds()
    writedelay = int(5)
    restart = (writedelay - differencetime)
    threading.Timer(restart, SerialRead).start() 

def main():
    app = QtGui.QApplication(sys.argv)
    form = ExampleApp()
    QtGui.QApplication.processEvents()
    form.show()
    app.exec_()    

if __name__ == '__main__':
    Thread(target = SerialRead).start()
    Thread(target = main).start()

1 个答案:

答案 0 :(得分:1)

我认为你可以做两件事之一:

  1. 在窗口中使用QTimer,将其间隔设置为5秒,并将方法连接到其超时信号,并更新该方法中的文本字段。
  2. 使用窗口和读取过程共享的threading event,并在窗口类中使用QTimer,检查事件是否已设置并在事件发生时进行更新。
  3. 我可能会使用一个事件,以便您知道线程正在休眠,并且在线程写入时您不会尝试读取值。

    ExampleApp课程中,您需要存储事件并处理超时:

    class ExampleApp(QtGui.QMainWindow, masimo.Ui_MainWindow):
        def __init__(self, event, parent=None):
            super(self.__class__, self).__init__()
            self.setupUi(self) 
            self.dataWasReadEvent = event
    
            self.checkThreadTimer = QtCore.QTimer(self)
            self.checkThreadTimer.setInterval(500) #.5 seconds
    
            self.checkThreadTimer.timeout.connect(self.readListValues)
    
        def readListValues(self):
            if self.dataWasReadEvent.is_set():
                #Read your events from the list and update your fields
    
                self.dataWasReadEvent.clear() #Clear the event set flag so that nothing happens the next time the timer times out
    

    您的SerialRead函数需要接受一个参数,即线程事件,并且需要在序列读取之后但在重新启动之前设置事件:

    def SerialRead(dataReadEvent):
        ...
        dataReadEvent.set()
        threading.Timer(restart, SerialRead, args=(dataReadEvent,)).start()
    

    您的main函数还需要接受一个事件参数传递给ExampleApp的初始值设定项:

    def main(dataReadEvent):
        ...
        form = ExampleApp(dataReadEvent)
    

    最后,在您的if __name__ == '__main__':部分中,需要创建线程事件并将其传递给线程调用:

    if __name__ == '__main__':
        dataReadEvent = threading.Event()
        Thread(target = SerialRead, args=(dataReadEvent,) ).start()
        Thread(target = main, args=(dataReadEvent,) ).start()