PyQt5 QDialog 在后续线程中

Posted

技术标签:

【中文标题】PyQt5 QDialog 在后续线程中【英文标题】:PyQt5 QDialog in subsequent threads 【发布时间】:2014-08-15 13:47:55 【问题描述】:

我在 Python 3.3 中有一个 PyQt5 程序,每次按下按钮时都会启动一个新线程。该线程将使用弹出对话框。它在第一次按下按钮时起作用,但是,第二次(在第一次完成之后)会使程序崩溃。我可以在线程中多次调用对话框,但第二次运行线程时程序会冻结。此代码将重现问题。

import sys
from threading import Thread
from PyQt5 import QtWidgets, QtCore


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(100, 100, 100, 50))
        self.pushButton.setObjectName("pushButton")

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

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Test"))
        self.pushButton.setText(_translate("Dialog", "OK"))


class Ui_MainWindow(object):
    def setupUi(self, mainWindow):
        mainWindow.setObjectName("mainWindow")
        self.pushButton = QtWidgets.QPushButton(mainWindow)
        self.pushButton.setGeometry(QtCore.QRect(30, 20, 100, 60))
        self.pushButton.setObjectName("pushButton")

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

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("mainWindow", "Test"))
        self.pushButton.setText(_translate("mainWindow", "Push Me!"))


class TestDialog(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(TestDialog, self).__init__(parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        # This message simply needs to go away when the button is pushed
        self.ui.pushButton.clicked.connect(self.close)

    def show_message(self):
        super(TestDialog, self).exec_()


class Main(QtWidgets.QMainWindow):
    def __init__(self):
        super(Main, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.dialog = TestDialog()

        self.ui.pushButton.clicked.connect(self.start_thread)

    def start_thread(self):
        t = Thread(target=self.show_dialog)
        t.daemon = True
        t.start()

    def show_dialog(self):
        # Do lots of background stuff here
        self.dialog.show_message()
        # The dialog can be shown multiple times within the same thread
        self.dialog.show_message()


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = Main()
    window.show()
    sys.exit(app.exec_())

删除对话框消息,它可以工作。那么为什么我不能从第二个线程调用对话框呢?我不是要同时运行两个线程,而是一个接一个地运行。

【问题讨论】:

为什么这么多线程??? 简而言之:你不应该在非主线程上创建 qwidgets:qt-project.org/doc/qt-5/… @mguijarr:这个想法是在后台对产品进行一些测试。我希望用户能够在测试完成后再次运行测试。 @sebastian 我很困惑。我不是在主线程中创建对话框吗?我以为我只是在线程中执行对话框。 您从新线程调用exec - 因此您直接从另一个线程访问小部件,而不是它所在的线程。这是你不应该做的。 【参考方案1】:

感谢塞巴斯蒂安的帮助,我想通了。我创建了一个信号对象,将它连接到 show_message 函数。我还添加了一个信号来告诉线程何时接受了对话。这是工作代码。

import sys
from threading import Thread
from PyQt5 import QtWidgets, QtCore


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(100, 100, 100, 50))
        self.pushButton.setObjectName("pushButton")

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

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Test"))
        self.pushButton.setText(_translate("Dialog", "OK"))


class Ui_MainWindow(object):
    def setupUi(self, mainWindow):
        mainWindow.setObjectName("mainWindow")
        self.pushButton = QtWidgets.QPushButton(mainWindow)
        self.pushButton.setGeometry(QtCore.QRect(30, 20, 100, 60))
        self.pushButton.setObjectName("pushButton")

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

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("mainWindow", "Test"))
        self.pushButton.setText(_translate("mainWindow", "Push Me!"))


class TestDialog(QtWidgets.QDialog):
    signal = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super(TestDialog, self).__init__(parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        # This message simply needs to go away
        self.ui.pushButton.clicked.connect(self.close)

    def show_message(self):
        # Use this to display the pop-up so the text can be altered
        super(TestDialog, self).exec_()
        self.signal.emit()


class Main(QtWidgets.QMainWindow):
    signal = QtCore.pyqtSignal()

    def __init__(self):
        super(Main, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.dialog = TestDialog()
        self.dialog_done = False

        self.ui.pushButton.clicked.connect(self.start_thread)

    def complete_dialog(self):
        self.dialog_done = True

    def wait_for_dialog(self):
        while not self.dialog_done:
            pass
        self.dialog_done = False

    def start_thread(self):
        t = Thread(target=self.show_dialog)
        t.daemon = True
        t.start()

    def show_dialog(self):
        # Do lots of background stuff here
        self.signal.emit()
        # Wait for the dialog to get closed
        self.wait_for_dialog()


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = Main()
    window.show()
    dialog = TestDialog()
    window.signal.connect(dialog.show_message)
    dialog.signal.connect(window.complete_dialog)
    sys.exit(app.exec_())

【讨论】:

【参考方案2】:

感谢@Brickmastr 发布的答案,我能够在稍微不同的场景中重新设计它以供我自己使用。如果有其他人想做我曾经做过的事情,希望这种略有不同的方法可以提供帮助。如果您正在运行一个程序,您希望在其中显示一个“正在运行”对话框,然后在该过程完成后更新该对话框,这是实现此目的的一种方法。此外,您可以通过这种方式将相同的弹出消息用于多个耗时的功能。

import PyQt5

#This below import is the python file that gets created from using QtDesigner
#And running pyuic5 to create a .py file from your .ui file - hopefully
#whomever reads this is familiar with using QtDesigner
import dialogBox as fxRun

#This is the file that would contain your primary UI, also created using QtDesigner
import mainUI
import threading

class MAIN_UI(PyQt5.QtWidgets.QMainWindow, mainUI.Ui_interface):
    startSignal = PyQt5.QtCore.pyqtSignal()
    endSignal = PyQt5.QtCore.pyqtSignal()

    def __init__(self,parent=None):
        super(MAIN_UI, self).__init__(parent)
        self.setupUi(self)
        self.buttonStartFunction1.clicked.connect(self.startFunction1)
        self.buttonStartFunction2.clicked.connect(self.startFunction2)

    def startFunction1(self):
        self.startThread(self.exampleMethod1)

   def startFunction2(self):
        self.startThread(self.exampleMethod2)

    def startThread(self,functionName):
        t = threading.Thread(target=functionName)
        t.daemon = True
        t.start()

    def exampleMethod1(self):
        #This function will show the dialog box at the beginning of the process
        # and will update the text and button once the process is complete
        FULLPROGRAM.mainUI.startSignal.emit()
        #Do lots of things here that take a long time
        FULLPROGRAM.mainUI.endSignal.emit()

    def exampleMethod2(self):
        #This can be a different function, just showing that you can send
        #whatever function into the startThread() method and it will work 
        #the same way 
        FULLPROGRAM.mainUI.startSignal.emit()
        #Do lots of things here that take a long time
        FULLPROGRAM.mainUI.endSignal.emit()

class PROCESS_BOX(PyQt5.QtWidgets.QDialog, fxRun.Ui_dialogBox):
    def __init__(self,parent=None): 
        super(PROCESS_BOX,self).__init__(parent)
        self.setupUi(self)
        self.buttonProcessCompleted.clicked.connect(self.close)

    def show_dialogbox(self): 
        self.setWindowTitle("RUNNING")
        self.labelProcessStatus.setText("PROCESSING REQUEST... \n PLEASE WAIT...")
        self.buttonProcessCompleted.setEnabled(False)
        super(PROCESS_BOX,self).exec_()

    def processComplete(self): 
        self.setWindowTitle("FINISHED")
        self.labelProcessStatus.setText("PROCESS COMPLETE! \n CLICK OK")
        self.buttonProcessCompleted.setEnabled(True)    

class FULLPROGRAM: 
    def __init__(self):
        app = PyQt5.QtWidgets.QApplication(sys.argv)
        FULLPROGRAM.fxRun = PROCESS_BOX()
        FULLPROGRAM.mainUI = MAIN_UI()
        FULLPROGRAM.mainUI.startSignal.connect(FULLPROGRAM.fxRun.show_dialogbox)
        FULLPROGRAM.mainUI.endSignal.connect(FULLPROGRAM.fxRun.processComplete)
        FULLPROGRAM.mainUI.show()
        app.exec_()

def main():
    program = FULLPROGRAM()

if __name__ == '__main__':
    main()

【讨论】:

以上是关于PyQt5 QDialog 在后续线程中的主要内容,如果未能解决你的问题,请参考以下文章

PyQt5 控件学习(一个一个学习之QDial)

如何在 PyQt5 Python 中的 QDial 上显示标记

PyQt5 - 显示来自不同类的 QDialog

在对话框之间切换(QDialog)PyQt5

关闭后执行的 PyQt5 QDialog 代码

PyQt5 控件学习(一个一个学习之QDialog)