每5秒更新一次文本框

2024-09-25 00:32:10 发布

您现在位置:Python中文网/ 问答频道 /正文

所以这是我的问题,我每5秒读取一次串行电缆的数据,并将其存储在一个CSV文件中。我也把这些数据整理成一个列表。我想做的是获取变量5、7和9,并将它们显示在我的GUI中,其中有qtextbox。。。我该怎么做?在

变量列表将位于一个名为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()

Tags: csvimportselfclosedatetimetimedtserial
1条回答
网友
1楼 · 发布于 2024-09-25 00:32:10

我想你可以做两件事之一:

  1. 在窗口中使用QTimer,将其间隔设置为5秒,并将方法连接到其超时信号,然后更新该方法中的文本字段。在
  2. 使用在窗口和读取进程之间共享的threading event,并在窗口类中使用一个QTimer,它可以更频繁地检查事件是否已设置,并在事件发生时进行更新。在

我可能会使用一个事件,以便您知道线程正在休眠,并且您不会在线程写入值时尝试读取值。在

在您的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函数需要接受一个参数,即线程事件,并且需要在串行读取之后重新启动之前设置该事件:

^{pr2}$

您的main函数还需要接受一个事件参数,以便传递给初始值设定项,例如app:

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()

相关问题 更多 >