PyQt5多个QObject是不应该定义的变量,

时间:2019-10-24 04:06:38

标签: python pyqt5

所以所有这些都来自Reddit线程,该线程显然对创建它的人有用,但对我不起作用。我不明白为什么它对我不起作用的问题

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

class Worker(QObject):
    finished = pyqtSignal()  # our signal out to the main thread to alert it we've completed our work

    def __init__(self):
        super(Worker, self).__init__()
        self.working = True  # this is our flag to control our loop

    def work(self):
        while self.working:
                print("I'm running")
                time.sleep(1)

        self.finished.emit() # alert our gui that the loop stopped


class Window(QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(50, 50, 800, 600)
        self.setWindowTitle("Program")
        self.setWindowIcon(QIcon('icon.png'))

        self.startbtn = QPushButton("Start", self)
        self.startbtn.resize(self.startbtn.minimumSizeHint())
        self.startbtn.move(100, 100)
        self.stopbtn = QPushButton("Stop", self)
        self.stopbtn.move(100, 200)
        self.stopbtn.resize(self.stopbtn.minimumSizeHint())

        self.thread = None
        self.worker = None

        self.startbtn.clicked.connect(self.start_loop)


    def start_loop(self):
        self.thread = QThread()  # a new thread to run our background tasks in
        self.worker = Worker()  # a new worker to perform those tasks
        self.worker.moveToThread(self.thread)  # move the worker into the thread, do this first before connecting the signals

        self.thread.started.connect(self.worker.work)  # begin our worker object's loop when the thread starts running
        self.stopbtn.clicked.connect(self.stop_loop)  # stop the loop on the stop button click
        self.worker.finished.connect(self.loop_finished)  # do something in the gui when the worker loop ends
        self.worker.finished.connect(self.thread.quit)  # tell the thread it's time to stop running
        self.worker.finished.connect(self.worker.deleteLater)  # have worker mark itself for deletion
        self.thread.finished.connect(self.thread.deleteLater)  # have thread mark itself for deletion
        # make sure those last two are connected to themselves or you will get random crashes

        self.thread.start()

    def stop_loop(self):
        self.worker.working = False
        # since thread's share the same memory, we read/write to variables of objects running in other threads
        # so when we are ready to stop the loop, just set the working flag to false


    def loop_finished(self):
        # received a callback from the thread that it completed
        print('Looped Finished')


def run():
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

# this is good practice as well, it allows your code to be imported without executing
if __name__ == '__main__': # then this script is being run directly,
    run()
else: # this script is being imported
    ... # usually you can leave off the else

所以我需要帮助的是试图解决这里发生的事情。我通过使用“开始”和“停止”按钮进行循环来使Gui变得更加复杂,但是我仍然遇到相同的错误,并且如果我能找出导致这些错误的原因,则可以在我的应用程序中完全解决该问题。

0 个答案:

没有答案