python - PyQt5 GUI 仅在屏幕关闭并重新打开时更新

标签 python qt user-interface pyqt pyqt5

当我通过串行接收值然后尝试用这些值更新我的 Gui 时遇到了问题。不幸的是,即使值正确更新,我也无法到达屏幕进行刷新,除非我单击它然后返回它。我尝试过重绘、更新和processEvents(),但无法解决问题。

这是我正在使用的代码:

import sys
import serial
import time
import requests


import PyQt5
from PyQt5.QtWidgets import *
from PyQt5.QtCore import*
from PyQt5.QtGui import *

import mainwindow_auto


CUSTOM_EVENT = 1000
ser = serial.Serial('/dev/ttyACM0', 9600)
class TestThread(QThread):

    def __init__(self, target):
        QThread.__init__(self)
        self.target = target

    def run(self):
        while True:
            QApplication.postEvent(self.target, QEvent(QEvent.Type(CUSTOM_EVENT)))
            QApplication.processEvents()
            QThread.sleep(15)




class MainWindow(QMainWindow, mainwindow_auto.Ui_MainWindow):

    def __init__(self):
        super(self.__class__, self).__init__()
        self.setupUi(self)# gets defined in the UI file
        self.thread = TestThread(self)
        self.thread.start()

    def event(s, e):
        if(e.type() == CUSTOM_EVENT):
            print("Readline: ",int(ser.readline()))
            SOC = int(ser.readline())
            s.lcdNumber.display(SOC)
            s.progressBar.setValue(SOC)
            print("SOC: ",SOC)
            print(s.lcdNumber.value())

        return True


def main():

    app = QApplication(sys.argv)
    form = MainWindow()
    form.lcdNumber.display(30)
    form.progressBar.setValue(30)
    form.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

提前致谢!

最佳答案

由于您已经有一个 I/O 线程,因此让它处理 I/O 并通过信号将接收到的值发送到主线程。

不需要自定义事件,主线程上没有 I/O。 只需向线程子类添加一个信号,并在启动线程之前将一个槽连接到该信号。

关于python - PyQt5 GUI 仅在屏幕关闭并重新打开时更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40374793/

相关文章:

python - Flask Python 日志记录 : TypeError while logging file path in File

python - 如何还原 Pycharm 中的更改

c++ - 更改 ItemData 的内容

user-interface - 如何让我的 Web 应用程序理解单数和复数之间的区别?

ios - UISlider 设置值

python - 如何将从 .arff 文件加载的 arff 对象转换为 dataframe 格式?

python - 无法更新词典

c++ - dll中的QT事件循环

c++ - 复制到 QMap 时出现 QString 错误。范围问题?

c++ - C++ 服务器的命令行界面