PyQt5:启动、停止和暂停具有进度更新的线程

PyQt5: start, stop and pause a thread with progress updates

我的程序中有线程问题。我有一个主程序和一个用 PyQt 创建的 GUI-Class。我已经为我的问题创建了一个小例子。 当我启动 python 脚本并按开始时,进度条工作正常,但我收到以下警告:

QBackingStore::endPaint() called with active painter on backingstore paint device
QObject::setParent: Cannot set parent, new parent is in a different thread

这些警报是由于不同的线程而发生的。但老实说,我不知道如何处理这些对象,比如 gui-class 的这个进度条。 我已经尝试了很多方法,但还没有找到一个好的解决方案。 按开始,它开始,按停止,进度条停止。但这是下一个问题,主要是当我按下停止键时我的内核死机了。我猜它与线程的相同问题有关?!

线程的原因是脚本在 运行ning 时仍然可以与之交互。

附件是我的两个 python 文件:我的程序和 gui。

主程序:

#MAINPROGRAM

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication
import sys
import GUI
import threading
import time

class exampleprogram(QtWidgets.QMainWindow, GUI.Ui_MainWindow):
    def __init__(self, parent=None):
        super(exampleprogram, self).__init__(parent)
        self.setupUi(self)
        self.pushButton_Start.clicked.connect(self.start)
        self.pushButton_Stop.clicked.connect(self.stop)
        self.running = False
            
    def start(self):
        if(self.running == False):
            print("Start")
            self.thread = threading.Thread(target=self.run, args=())
            self.thread.start()            
            
    def stop(self):
        print("Stop")
        self.running = False
        
    def run(self):
        self.running = True
        x = 0
        thread = threading.currentThread()
        while getattr(thread, "do_run", True):
            self.thread.do_run = self.running
            if(x == 100):
                thread.do_run = False
            self.progressBar.setValue(x)
            time.sleep(0.1)
            x = x+1
        self.stop()

def main():
    app = QApplication(sys.argv)
    form = exampleprogram()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

界面:

#GUI

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(573, 92)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.layoutWidget.setGeometry(QtCore.QRect(20, 20, 195, 30))
        self.layoutWidget.setObjectName("layoutWidget")
        self.formLayout_3 = QtWidgets.QFormLayout(self.layoutWidget)
        self.formLayout_3.setContentsMargins(0, 0, 0, 0)
        self.formLayout_3.setObjectName("formLayout_3")
        self.pushButton_Start = QtWidgets.QPushButton(self.layoutWidget)
        self.pushButton_Start.setObjectName("pushButton_Start")
        self.formLayout_3.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.pushButton_Start)
        self.pushButton_Stop = QtWidgets.QPushButton(self.layoutWidget)
        self.pushButton_Stop.setObjectName("pushButton_Stop")
        self.formLayout_3.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.pushButton_Stop)
        self.progressBar = QtWidgets.QProgressBar(self.centralwidget)
        self.progressBar.setGeometry(QtCore.QRect(230, 20, 311, 23))
        self.progressBar.setProperty("value", 0)
        self.progressBar.setObjectName("progressBar")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Test Gui"))
        self.pushButton_Start.setText(_translate("MainWindow", "Start"))
        self.pushButton_Stop.setText(_translate("MainWindow", "Stop"))


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_())

有没有人知道如何解决?

编辑:停止 运行 功能后,应该可以通过按开始按钮再次 运行 它。因此,它是被计数器 x 停止还是被按下停止按钮停止并不重要。重要的是不要多次启动该功能。

非常感谢!

安德鲁

虽然可以在 PyQt 中使用 Python 线程,但通常最好将 QThread 与单独的工作对象一起使用,因为它更容易允许工作线程之间的 thread-safe 通信和主线程通过信号和插槽。您的示例的主要问题之一恰恰是您试图在主线程之外执行 GUI-related 操作,Qt 不支持该操作(因此出现警告消息)。

如果您想从 GUI 启动、停止和暂停工作程序,则需要小心处理以确保在用户尝试关闭工作程序和线程时干净地关闭工作程序和线程 window仍然 运行。在下面的示例中,我在 closeEvent; if you want more sophisticated handling, you could, for example, ignore() 和 close-event 中使用了一个简单的中止机制,而 worker 仍然是 运行。按 Start 将 start/contine worker,按 Stop 将暂停它。工作人员完成后,按 Start 将完全 re-start 它(即它会回到零):

class Worker(QtCore.QObject):
    progress = QtCore.pyqtSignal(int)
    finished = QtCore.pyqtSignal()

    def __init__(self):
        super().__init__()
        self._paused = True
        self._count = -1

    def start(self):
        self._paused = False
        if self._count < 0:
            print('Start')
            self._run()
        else:
            print('Continue')

    def stop(self, *, abort=False):
        self._paused = True
        if abort:
            print('Abort')
            self._count = -1
        else:
            print('Pause')

    def _run(self):
        self._count = 0
        self._paused = False
        while 0 <= self._count <= 100:
            if not self._paused:
                QtCore.QThread.msleep(100)
                self._count += 1
                self.progress.emit(self._count)
        self.stop(abort=True)
        self.finished.emit()
        print('Finished')


class exampleprogram(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(exampleprogram, self).__init__(parent)
        self.setupUi(self)
        self.thread = QtCore.QThread(self)
        self.worker = Worker()
        self.worker.moveToThread(self.thread)
        self.thread.started.connect(self.worker.start)
        self.worker.finished.connect(self.thread.quit)
        self.worker.progress.connect(self.progressBar.setValue)
        self.pushButton_Start.clicked.connect(self.start)
        self.pushButton_Stop.clicked.connect(self.stop)

    def start(self):
        if self.thread.isRunning():
            self.worker.start()
        else:
            self.thread.start()

    def stop(self):
        if self.thread.isRunning():
            self.worker.stop()

    def closeEvent(self, event):
        self.worker.stop(abort=True)
        self.thread.quit()
        self.thread.wait()