簡體   English   中英

PyQt5 QDialog在后續線程中

[英]PyQt5 QDialog in subsequent threads

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

刪除對話框消息,它起作用。 那為什么不能從第二個線程調用對話框呢? 我不是試圖同時運行兩個線程,而是一個接一個。

感謝塞巴斯蒂安的幫助,我找到了答案。 我創建了一個信號對象,將其連接到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_())

感謝@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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM