在某些PyQt5 QThread类完成工作后运行Python函数的最佳方法是什么?

时间:2018-03-12 13:18:34

标签: python-3.x pyqt5 qthread

我使用PyQt5和Python3,我使用3个QThread类来运行某些东西,完成后我需要执行第4个QThread类。但是第四个的执行需要在所有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_())

我的前3个QThreads是这样的:

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

我上次的QThread看起来像这样:

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都会启动,我希望我的第4个QThread只能在前3个QThread完成后启动。我使用QThreads来改善GUI体验,GUI冻结了很多。

感谢,

1 个答案:

答案 0 :(得分:1)

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