如何在Python中为GUI更新Qt标签?

问题描述

我用Qt设计器设计了一个简单的GUI,目标是显示一个不断变化的变量(汽车的RPM)。 GUI已经转换为Python代码。问题在于,我不知道如何在不关闭然后重新打开窗口的情况下更新窗口。

我希望GUI不断显示最新值。

这是我到目前为止尝试过的:

from PyQt5 import QtCore,QtGui,QtWidgets
import sys
import platform
from PySide2 import QtCore,QtWidgets
from PySide2.QtCore import (QCoreApplication,QPropertyAnimation,QDate,QDateTime,QMetaObject,QObject,QPoint,QRect,QSize,QTime,QUrl,Qt,QEvent)
from PySide2.QtGui import (QBrush,QColor,QConicalGradient,QCursor,QFont,QFontDatabase,QIcon,QKeySequence,QLinearGradient,QPalette,QPainter,QPixmap,QRadialGradient)
from PySide2.QtWidgets import *
import os
import cantools
import can

os.system("sudo /sbin/ip link set can0 up type can bitrate 1000000")
db = cantools.db.load_file('/home/pi/Documents/secret') 
can_bus = can.interface.Bus('can0',bustype='socketcan',bitrate= 1000000)
class Ui_MainWindow(object):
def setupUi(self,MainWindow):
    MainWindow.setObjectName("MainWindow")
    MainWindow.resize(800,480)
        
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    Unimportant info...
    self.label.setFont(font)
    self.label.setStyleSheet("color: rgb(244,244,244);")
    self.label.setObjectName("label")
    self.label_2 = QtWidgets.QLabel(self.frame)
    self.label_2.setGeometry(QtCore.QRect(390,120,161,51))
    font = QtGui.QFont()
    font.setPointSize(23)
    self.label_2.setFont(font)
    self.label_2.setStyleSheet("color: rgb(244,244);")
    self.label_2.setObjectName("label_2")
    MainWindow.setCentralWidget(self.centralwidget)
        
    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)
        
        ################### REMOVE Window bar
    MainWindow.setWindowFlag(QtCore.Qt.FramelessWindowHint)
    MainWindow.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        
def retranslateUi(self,MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow","MainWindow"))
    self.label.setText(_translate("MainWindow","<strong> RPM"))

## Gets the CAN bus data and gets RPM

    while True:
        message = can_bus.recv()
        if (message.arbitration_id == 40)
            decodedMessage = db.decode_message(message.arbitration_id,message.data)            
            if (message.arbitration_id == 40):
                rpm = decodedMessage.get('RPM')
                print(rpm)
####### Label to update
                    self.label_2.setText(str(rpm))
# MainWindow","<html><head/><body><p><span style=\" font-weight:600;\">VALUE</span></p></body></html>

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

不要过多地关注所有代码,但重要的部分是双#部分。这是决定汽车转速的地方,也是最有可能出现问题的地方。当我运行此代码时,只要收到RPM,它就会尝试不断打开并关闭窗口。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...