如何实时更新PyQt5标签?

How to update PyQt5 label in real time?

大家好,我目前正在开发每 15 秒更新一次的 GUI。我对 Python 也很陌生,所以我正在寻找一些可以在这里获得的指导。

我从每 15 秒更新一次的 .txt 文件获取数据,所以现在我尝试每 15 秒将这些数据输入 GUI。它有效,但运行几次后,在我的提示下出现错误代码,

QEventDispatcherWin32::registerTimer: Failed to create a timer (The current process has used all of its system allowance of handles for Window Manager objects.)

GUI 仍将每 15 秒更新一次,但那个错误让我觉得我已经做错了。 我想知道是不是因为我一直在我的袖口循环中创建新的计时器?

这是我的 GUI 的编码。

from PyQt5 import QtCore, QtGui, QtWidgets, uic
import sys
import time

class dataProcessing(QtWidgets.QMainWindow):
    def __init__(self):
        super(dataProcessing,self).__init__()
        uic.loadUi('CuffingEfficiency2.ui',self)
        self.show()
        self.Cuff()

    def Cuff(self):
        with open('Cuffing.txt', 'r') as r:
                l1,l2,l3,l4,l5,l6 = [float(i) for i in r.readlines()]
                self.label_8.setText(str(l1))
                self.label_9.setText(str(l3))
                self.label_12.setText(str(l5))
                self.label_13.setText(str(l2))
                self.label_10.setText(str(l4))
                self.label_11.setText(str(l6))
                           
        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(self.Cuff)
        self.timer.start(15000)     
                

app = QtWidgets.QApplication(sys.argv)
window = dataProcessing()
app.exec_()

谢谢!

警告可能是因为您在每次执行“Cuff”时都创建了一个新的 QTimer,在您的情况下,一个 QTimer 就足够了:

class dataProcessing(QtWidgets.QMainWindow):
    def __init__(self):
        super(dataProcessing, self).__init__()
        uic.loadUi("CuffingEfficiency2.ui", self)
        self.show()
        timer = QtCore.QTimer(self, timeout=self.Cuff, interval=15 * 1000)
        timer.start()
        self.Cuff()

    def Cuff(self):
        labels = (
            self.label_8,
            self.label_9,
            self.label_12,
            self.label_13,
            self.label_10,
            self.label_11,
        )
        with open("Cuffing.txt", "r") as r:
            for label, line in zip(
                labels,
                r.readlines(),
            ):
                try:
                    label.setNum(float(line))
                except ValueError:
                    pass