【问题标题】:PyQT5: wait for signal before continuing the functionPyQT5:在继续功能之前等待信号
【发布时间】:2020-12-24 23:33:57
【问题描述】:

使用 PyQt5 作为我的程序的接口,我编写了一个通过单击按钮激活的函数,该函数激活(除其他外)另一个函数,该函数激活一个线程,该线程在第一个函数完成它的工作时工作在其他东西上. 我的问题是,函数完成的速度比线程快得多,我希望程序在继续之前等待线程结束,最好使用超时选项。 我知道 PyQT5 中有信号,并且我知道如何根据给定的信号触发事件,但是,我如何让程序等待信号到来?

非常感谢您的帮助:)

我想要的示例代码:

class MyClass():
    def __init__():
        self.first_function()
        ....
        ....

    def first_function(self):
        ....
        ....
        self.second_function()
        wait_for_signal(timeout=30)

    def second_function(self):
        ....
        t = threading.Thread(target=self.thread_func)
        t.start()
        ....
        ....

    def thread_func(self):
        ....
        ....
        signal.emit()

【问题讨论】:

    标签: python pyqt5 signals


    【解决方案1】:

    我不知道你到底需要什么,但这里有一个函数示例,它在开始块发出信号并最终激活它们

    def show_acte_general(self):
            
            self.comboBox_2.blockSignals(True)
            self.comboBox_2.clear()
            self.comboBox_4.blockSignals(True)
            self.comboBox_4.clear()
            
            self.connection = psycopg2.connect(user="postgres", password="password", host="localhost", database="database")
            self.cur = self.connection.cursor()
            self.cur.execute('''SELECT items FROM read ORDER BY items ASC;''')
            data = self.cur.fetchall()
    
            for actes in data :
                self.comboBox_2.addItem(actes[0])
                self.comboBox_2.blockSignals(False)
                
                self.comboBox_4.addItem(actes[0])
                self.comboBox_4.blockSignals(False)
    
    

    【讨论】:

      猜你喜欢
      • 2020-01-15
      • 2018-04-19
      • 1970-01-01
      • 2018-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      相关资源
      最近更新 更多