Python 3.x 在某些PyQT5QThread类完成工作后,运行Python函数的最佳方式是什么?

Python 3.x 在某些PyQT5QThread类完成工作后,运行Python函数的最佳方式是什么?,python-3.x,pyqt5,qthread,Python 3.x,Pyqt5,Qthread,我使用PyQt5和Python3,我使用3个QThread类来运行一些东西,在它们完成后,我需要执行第4个QThread类。但是第4个类的执行需要在所有QThread类完成工作之后进行,或者只有2个或只有1个。当前3个正在工作时,它不能运行。 我在网上查了一下,但找不到解决办法。我的代码如下所示: class MyWindow(QtWidgets.QMainWindow): def __init__(self): QtWidgets.QMainWindo

我使用PyQt5和Python3,我使用3个QThread类来运行一些东西,在它们完成后,我需要执行第4个QThread类。但是第4个类的执行需要在所有QThread类完成工作之后进行,或者只有2个或只有1个。当前3个正在工作时,它不能运行。 我在网上查了一下,但找不到解决办法。我的代码如下所示:

 class MyWindow(QtWidgets.QMainWindow):
        def __init__(self):
            QtWidgets.QMainWindow.__init__(self)
            file_path = os.path.abspath('builder_gui.ui')
            uic.loadUi(file_path, self)
            self.obj1 = TasksThread1(self.comboBox.currentText(),self.comboBox_6.currentText())
            self.obj2 = TasksThread2(self.comboBox_2.currentText(),self.comboBox_5.currentText())
            self.obj3 = TasksThread3(self.comboBox_3.currentText(),self.comboBox_4.currentText())
            self.obj4 = TasksThread4()
            self.menubar.setNativeMenuBar(False)
            self.progressVal = 1
            self.cwd = os.getcwd()
            self.obj1.newValueProgress.connect(self.increment_progress)
            self.obj1.message.connect(self.status_bar)
            self.obj2.newValueProgress.connect(self.increment_progress)
            self.obj2.message.connect(self.status_bar)
            self.obj3.newValueProgress.connect(self.increment_progress)
            self.obj3.message.connect(self.status_bar)
            self.obj4.newValueProgress.connect(self.increment_progress)
            self.obj4.message.connect(self.status_bar)
            self.obj4.doneSignal.connect(self.calculate_done_limit)
            self.pushButton.pressed.connect(self.execute_build_script)
    def calculate_done_limit(self):
        limitCalc = 100 - int(self.progressBar.value())
        self.increment_progress(limitCalc)

    def run_gits_all(self):
        if self.crowdTwistCheck.isChecked():
            self.obj1.start()
        else:
            pass
        if self.ThemeCheck.isChecked():
            self.obj2.start()
        else:
            pass
        if self.mainAwsCheck.isChecked():
            self.obj3.start()
        else:
            pass

    def execute_build_script(self):
        self.progressBar.setValue(1)
        self.progressVal = 1
        self.run_gits_all()

    def execute_last_part(self):
        self.obj4.start()

    def status_bar(self, value_in):
        read1 = self.textBrowser.toPlainText()
        self.textBrowser.setText(read1 + "\n" + value_in)

    def increment_progress(self,valueIn):
        self.progressVal += valueIn
        self.progressBar.setValue(self.progressVal)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())
class TasksThread1(QThread):
    newValueProgress = QtCore.pyqtSignal(int)
    message = QtCore.pyqtSignal(str)
    doneSignal = QtCore.pyqtSignal()
    def __init__(self, branch, git):
        QThread.__init__(self)
        self.branch = branch
        self.git = git

    def remove_folder(self):
        do_something_1

    def CrowdTwistRepo(self):
        do_something_2

    def run(self):
        self.remove_folder()
        self.CrowdTwistRepo()
class TasksThread4(QThread):
    newValueProgress = QtCore.pyqtSignal(int)
    message = QtCore.pyqtSignal(str)
    doneSignal = QtCore.pyqtSignal()
    def __init__(self):
        QThread.__init__(self)

    def gulp_sass_function(self):
        do_something_1

    def gulp_uglify_function(self):
        do_something_2

    def zipping_function(self):
        do_something_3

    def run(self):
        self.gulp_sass_function()
        self.gulp_uglify_function()
        self.zipping_function()
我的前3个QThread如下所示:

 class MyWindow(QtWidgets.QMainWindow):
        def __init__(self):
            QtWidgets.QMainWindow.__init__(self)
            file_path = os.path.abspath('builder_gui.ui')
            uic.loadUi(file_path, self)
            self.obj1 = TasksThread1(self.comboBox.currentText(),self.comboBox_6.currentText())
            self.obj2 = TasksThread2(self.comboBox_2.currentText(),self.comboBox_5.currentText())
            self.obj3 = TasksThread3(self.comboBox_3.currentText(),self.comboBox_4.currentText())
            self.obj4 = TasksThread4()
            self.menubar.setNativeMenuBar(False)
            self.progressVal = 1
            self.cwd = os.getcwd()
            self.obj1.newValueProgress.connect(self.increment_progress)
            self.obj1.message.connect(self.status_bar)
            self.obj2.newValueProgress.connect(self.increment_progress)
            self.obj2.message.connect(self.status_bar)
            self.obj3.newValueProgress.connect(self.increment_progress)
            self.obj3.message.connect(self.status_bar)
            self.obj4.newValueProgress.connect(self.increment_progress)
            self.obj4.message.connect(self.status_bar)
            self.obj4.doneSignal.connect(self.calculate_done_limit)
            self.pushButton.pressed.connect(self.execute_build_script)
    def calculate_done_limit(self):
        limitCalc = 100 - int(self.progressBar.value())
        self.increment_progress(limitCalc)

    def run_gits_all(self):
        if self.crowdTwistCheck.isChecked():
            self.obj1.start()
        else:
            pass
        if self.ThemeCheck.isChecked():
            self.obj2.start()
        else:
            pass
        if self.mainAwsCheck.isChecked():
            self.obj3.start()
        else:
            pass

    def execute_build_script(self):
        self.progressBar.setValue(1)
        self.progressVal = 1
        self.run_gits_all()

    def execute_last_part(self):
        self.obj4.start()

    def status_bar(self, value_in):
        read1 = self.textBrowser.toPlainText()
        self.textBrowser.setText(read1 + "\n" + value_in)

    def increment_progress(self,valueIn):
        self.progressVal += valueIn
        self.progressBar.setValue(self.progressVal)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())
class TasksThread1(QThread):
    newValueProgress = QtCore.pyqtSignal(int)
    message = QtCore.pyqtSignal(str)
    doneSignal = QtCore.pyqtSignal()
    def __init__(self, branch, git):
        QThread.__init__(self)
        self.branch = branch
        self.git = git

    def remove_folder(self):
        do_something_1

    def CrowdTwistRepo(self):
        do_something_2

    def run(self):
        self.remove_folder()
        self.CrowdTwistRepo()
class TasksThread4(QThread):
    newValueProgress = QtCore.pyqtSignal(int)
    message = QtCore.pyqtSignal(str)
    doneSignal = QtCore.pyqtSignal()
    def __init__(self):
        QThread.__init__(self)

    def gulp_sass_function(self):
        do_something_1

    def gulp_uglify_function(self):
        do_something_2

    def zipping_function(self):
        do_something_3

    def run(self):
        self.gulp_sass_function()
        self.gulp_uglify_function()
        self.zipping_function()
我的最后一个QThread如下所示:

 class MyWindow(QtWidgets.QMainWindow):
        def __init__(self):
            QtWidgets.QMainWindow.__init__(self)
            file_path = os.path.abspath('builder_gui.ui')
            uic.loadUi(file_path, self)
            self.obj1 = TasksThread1(self.comboBox.currentText(),self.comboBox_6.currentText())
            self.obj2 = TasksThread2(self.comboBox_2.currentText(),self.comboBox_5.currentText())
            self.obj3 = TasksThread3(self.comboBox_3.currentText(),self.comboBox_4.currentText())
            self.obj4 = TasksThread4()
            self.menubar.setNativeMenuBar(False)
            self.progressVal = 1
            self.cwd = os.getcwd()
            self.obj1.newValueProgress.connect(self.increment_progress)
            self.obj1.message.connect(self.status_bar)
            self.obj2.newValueProgress.connect(self.increment_progress)
            self.obj2.message.connect(self.status_bar)
            self.obj3.newValueProgress.connect(self.increment_progress)
            self.obj3.message.connect(self.status_bar)
            self.obj4.newValueProgress.connect(self.increment_progress)
            self.obj4.message.connect(self.status_bar)
            self.obj4.doneSignal.connect(self.calculate_done_limit)
            self.pushButton.pressed.connect(self.execute_build_script)
    def calculate_done_limit(self):
        limitCalc = 100 - int(self.progressBar.value())
        self.increment_progress(limitCalc)

    def run_gits_all(self):
        if self.crowdTwistCheck.isChecked():
            self.obj1.start()
        else:
            pass
        if self.ThemeCheck.isChecked():
            self.obj2.start()
        else:
            pass
        if self.mainAwsCheck.isChecked():
            self.obj3.start()
        else:
            pass

    def execute_build_script(self):
        self.progressBar.setValue(1)
        self.progressVal = 1
        self.run_gits_all()

    def execute_last_part(self):
        self.obj4.start()

    def status_bar(self, value_in):
        read1 = self.textBrowser.toPlainText()
        self.textBrowser.setText(read1 + "\n" + value_in)

    def increment_progress(self,valueIn):
        self.progressVal += valueIn
        self.progressBar.setValue(self.progressVal)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())
class TasksThread1(QThread):
    newValueProgress = QtCore.pyqtSignal(int)
    message = QtCore.pyqtSignal(str)
    doneSignal = QtCore.pyqtSignal()
    def __init__(self, branch, git):
        QThread.__init__(self)
        self.branch = branch
        self.git = git

    def remove_folder(self):
        do_something_1

    def CrowdTwistRepo(self):
        do_something_2

    def run(self):
        self.remove_folder()
        self.CrowdTwistRepo()
class TasksThread4(QThread):
    newValueProgress = QtCore.pyqtSignal(int)
    message = QtCore.pyqtSignal(str)
    doneSignal = QtCore.pyqtSignal()
    def __init__(self):
        QThread.__init__(self)

    def gulp_sass_function(self):
        do_something_1

    def gulp_uglify_function(self):
        do_something_2

    def zipping_function(self):
        do_something_3

    def run(self):
        self.gulp_sass_function()
        self.gulp_uglify_function()
        self.zipping_function()
如果我运行代码,所有的QThread都会启动,我希望我的第四个QThread只在前3个QThread完成工作后启动。我使用QThreads来改善GUI体验,GUI冻结了很多


谢谢,

完成前3个线程后,发送一个信号。然后将此信号连接到将启动最后一个线程的函数。

我发出了3个信号,每个QThread有一个doneSignal。但是第一个完成的将运行第四个QThread,我不希望这样。只需创建3个标志。仅当设置了3个标志时才启动第4个。标志是好的,但在你的情况下,QSemaphore可能更合适谢谢你的想法,我从来没有使用过QSemaphore,我是Qt的新手。我明天会试试,并带回一个答案。老实说,我建议每次调用插槽时只递减一个整数。然后,如果整数已递减3次,则仅启动第4个线程。当它为空时,启动另一个线程。