目前,我可以运行一个线程,并在线程运行时更新GUI(pyqt5)中的标签项。但是,我想添加有关线程状态的信息,即线程何时开始(标签更新为:'thread initiated')和线程何时结束(标签更新为:'thread complete')。线程启动更新不是问题,因为我可以在线程启动后立即更新标签。但是,线程结束更新有点问题,因为我无法知道线程何时完成。我尝试了thread.join(),不过这会阻止线程运行时标签的动态更新(GUI会冻结,直到线程完成)。我试图从一个单独的函数更新标签,但没有成功
我的当前代码(简称):
def onbuttonpress(self):
self.label.setText('thread initiated')
self.simulation()
self.label.setText('thread finished')
def simulation(self):
def sim():
pythoncom.CoInitialize()
------code-----
self.thread = threading.Thread(target=mt)
self.thread.start()
除了thread.join()之外,还有其他方法可以在不破坏GUI的情况下工作吗
您可以使用
join()
的timeout参数来尝试加入,如果不成功,它还没有准备好https://docs.python.org/2/library/threading.html#threading.Thread.join
在超时期间,GUI线程将被阻塞,因此将其设置为一个较小的值(例如1毫秒)。定期执行此检查(例如,每秒一次),直到线程完成
或者,使用Qt的signal slot机制从工作线程通知GUI线程
pyqt4 emiting signals in threads to slots in main thread
PyQt5 - How to emit signal from worker tread to call event by GUI thread
相关问题 更多 >
编程相关推荐