def process(self):
self._status = "processing image 1"
....
def _update(self):
self.status_label.setText(self._status)
def start_processing(self, image_path):
# create thread for process and run it
# create thread for updating by using QtCore.QTimer()
# connect qtimer triggered signal to and `self._update()` slot
# connect image processing thread (use connect signal to any slot, in this example I'll stop timer after processing thread finishes)
@pyqtSlot()
def _stop_timer():
self._qtimer.stop()
self._qtimer = None
_update_thread.finished.connect(_stop_timer)
如果没有多线程,我无法实现您需要的东西,这是不可能的,因为gui只能在主线程中更新。下面是一个算法,我是如何做到这一点与多线程。在
假设您的应用程序正在处理图像。然后是以下线程:
在你的电脑上处理图像的线程
在pyqt5中,可以从一个嵌套线程(第一级)分配pyqtvariable。因此,您可以使用setter和getter使变量成为pyqtvariable,并在setter中更新gui,或者思考如何自己完成这项工作。在
是的,您可以很容易地使用processEvents来实现这一点,这正是为此目的而提供的。在
我用这个技术实现了一个简单的“在文件中查找”对话框。您需要做的就是启动一个函数,该函数用single-shot timer处理文件,然后在循环中周期性地调用
processEvents
。这应该足够好,以更新计数器处理的文件数,也允许用户取消进程,如果必要的话。在唯一真正的问题是决定调用
processEvents
的频率。您调用它的频率越高,GUI的响应就越快—但这是以大大减慢文件处理速度为代价的。所以你可能需要做一点实验,以便找到一个可以接受的折衷方案。在更新:
下面是一个简单的演示,演示如何构造代码:
您只需使用python
threading
模块,并在线程例程中发出一个信号。在这是一个有效的例子
相关问题 更多 >
编程相关推荐