如何在PyQt中使用标准线程?可能吗?

时间:2019-08-29 12:52:56

标签: python multithreading pyqt pyqt5

我将python代码重写为Qt图形应用程序。一切正常,但现在Qt出现了问题。

有效的我的简化python代码段:

function1():
      status = checkBox.value
      ct.run = status


def function2():
        ct = threading.currentThread()
        while True:
            if getattr(t, "run", False):
                print("test")
                sleep(0.3)

t = threading.Thread(target = self.function2)
t.start()

我在PyQt中遇到问题的代码

class MainWindow(QtWidgets.QMainWindow, mainwindow_auto.Ui_MainWindow):
    def __init__(self, parent = None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)

        self.checkBox.toggled.connect(self.function1)
        t = threading.Thread(target = self.function2)
        t.start()


    def function1(self):
        status = self.sender().isChecked()
        ct.run = status
        print(status)


    def function2(self):
        ct = threading.currentThread()
        while True:
            while getattr(t, "run", False):
                print("test")
                sleep(0.3)

消息:

Traceback (most recent call last):
  File "/home/pi/Desktop/chechBox/main_2.py", line 28, in function1
    t.run = status
NameError: name 't' is not defined
Backend terminated (returncode: -6)
Fatal Python error: Aborted

在PyQt中甚至有可能这样做吗?

0 个答案:

没有答案