window 当我尝试使用 python 的线程在 Qt window 中显示不断变化的值时冻结

window freezes when I try to show an evolving value in a Qt window using threads with python

我的主要objective是在Qt-windowtextEdit上展示一个不断发展的价值。 (这个 window 只包含一个 checkBox 和一个 textEdit)。 遗憾的是,我无法单击该复选框并且 window 被冻结,直到我关闭终端。

import sys
from threading import Thread
from random import randint
import time
from PyQt4 import QtGui,uic


class MyThread(Thread):

    def __init__(self):
        Thread.__init__(self)

    #function to continually change the targeted value
    def run(self):
        for i in range(1, 20):
            self.a = randint (1, 10)
            secondsToSleep = 1
            time.sleep(secondsToSleep)


class MyWindow(QtGui.QMainWindow,Thread):
    def __init__(self):
        Thread.__init__(self)
        super(MyWindow,self).__init__()
        uic.loadUi('mywindow.ui',self)
        self.checkBox.stateChanged.connect(self.checkeven)
        self.show()

    #i show the value only if the checkbox is checked
    def checkeven(self):
        while self.checkBox.isChecked():
            self.textEdit.setText(str(myThreadOb1.a)) 



# Run following code when the program starts
if __name__ == '__main__':
   app = QtGui.QApplication(sys.argv)


   # Declare objects of MyThread class
   myThreadOb1 = MyThread()
   myThreadOb2 = MyWindow()

   # Start running the threads!
   myThreadOb1.start()
   myThreadOb2.start()

   sys.exit(app.exec_())

目前我正在使用线程将随机值设置为 a,但最后应该会更复杂一些,因为我必须从自动化中获取值.

你知道为什么我的代码会这样吗? 非常感谢您的帮助。

问题是 while self.checkBox.isChecked() 正在阻塞,阻止 GUI 处理其他事件。

此外,您不应该 运行 在主线程之外的另一个线程上运行 PyQt GUI。

如果您想将数据从一个线程发送到另一个线程,一个不错的选择是使用信号。

考虑到所有这些因素,我们有以下几点:

import sys
from threading import Thread
from random import randint
import time
from PyQt4 import QtGui, uic, QtCore


class MyThread(Thread, QtCore.QObject):
    aChanged = QtCore.pyqtSignal(int)

    def __init__(self):
        Thread.__init__(self)
        QtCore.QObject.__init__(self)
    #function to continually change the targeted value
    def run(self):
        for i in range(1, 20):
            self.aChanged.emit(randint(1, 10))
            secondsToSleep = 1
            time.sleep(secondsToSleep)


class MyWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MyWindow,self).__init__()
        uic.loadUi('mywindow.ui',self)
        self.thread = MyThread()
        self.thread.aChanged.connect(self.on_a_changed, QtCore.Qt.QueuedConnection)
        self.thread.start()
        self.show()

    #i show the value only if the checkbox is checked
    @QtCore.pyqtSlot(int)
    def on_a_changed(self, a):
        if self.checkBox.isChecked():
            self.textEdit.setText(str(a)) 



# Run following code when the program starts
if __name__ == '__main__':
   app = QtGui.QApplication(sys.argv)


   # Declare objects of MyThread class
   w = MyWindow()

   sys.exit(app.exec_())

更符合 Qt 风格的选项是使用 QThread,因为它是一个 class 处理线程并且是一个 QObject,因此它可以轻松处理信号。

import sys
from random import randint
from PyQt4 import QtGui, uic, QtCore


class MyThread(QtCore.QThread):
    aChanged = QtCore.pyqtSignal(int)
    def run(self):
        for i in range(1, 20):
            self.aChanged.emit(randint(1, 10))
            secondsToSleep = 1
            QtCore.QThread.sleep(secondsToSleep)


class MyWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MyWindow,self).__init__()
        uic.loadUi('mywindow.ui',self)
        self.thread = MyThread()
        self.thread.aChanged.connect(self.on_a_changed, QtCore.Qt.QueuedConnection)
        self.thread.start()
        self.show()

    #i show the value only if the checkbox is checked
    @QtCore.pyqtSlot(int)
    def on_a_changed(self, a):
        if self.checkBox.isChecked():
            self.textEdit.setText(str(a)) 



# Run following code when the program starts
if __name__ == '__main__':
   app = QtGui.QApplication(sys.argv)


   # Declare objects of MyThread class
   w = MyWindow()

   sys.exit(app.exec_())