QThread 无法正常工作并且 pyqt gui 应用程序冻结
QThread not work correctly and pyqt gui application freeze
我正在编写 python gui 应用程序来处理图像并从串行端口发送图像颜色并显示结果,但不幸的是我的 gui 冻结了。我尝试使用 QApllication.processEvents 并且它可以工作但是我的程序速度很慢而且速度对我来说非常重要并且每一秒一次迭代应该完成然后我使用 QThread 并且我的应用程序仍然冻结。这是我的代码:
class Worker(QObject):
progress = pyqtSignal(int)
gui_update = pyqtSignal()
def __init__(self, knot, lay, baft):
super().__init__()
self.knot = knot
self.lay = lay
self.baft = baft
def run(self):
while self.knot <= knotter_width:
color_to_send = []
for i in range(1, number_of_knotter + 1):
color_to_send.append(self.baft["knotters"][i][self.lay][self.knot])
self.progress.emit(self.knot)
self.gui_update.emit() # for updating gui but not work
QThread 设置:
self.thrd = QThread()
self.worker = Worker(self.knot, self.lay, self.baft)
self.worker.moveToThread(self.thrd)
self.thrd.started.connect(self.worker.run)
self.worker.progress.connect(self.progress)
self.worker.gui_update.connect(self.knotters_status)
self.worker.finish.connect(self.finished)
self.worker.ex.connect(self.thrd.quit)
self.worker.ex.connect(self.worker.deleteLater)
self.thrd.finished.connect(self.thrd.deleteLater)
self.thrd.start()
经过三天的研究,我发现在设置线程后,您需要在创建线程对象后立即调用 processEvents(),在这种情况下,它不会导致速度变慢每件事都很完美。
注意:您应该将参数放在 init 函数中。
这是我的最终代码:
class Thrd(QThread):
progress = pyqtSignal(int)
gui_update = pyqtSignal()
finish = pyqtSignal(bool)
ex = pyqtSignal()
def __init__(self, knot, lay, baft):
super().__init__()
self.knot = knot
self.lay = lay
self.baft = baft
def run(self):
while condition:
# some process
self.progress.emit(self.knot)
self.gui_update.emit()
time.sleep(0.05) # give a little time to update gui
self.finish.emit(True)
self.ex.emit()
和设置部分:
self.thrd = Thrd(self.knot, self.lay, self.baft)
app.processEvents()
self.thrd.progress.connect(self.progress)
self.thrd.gui_update.connect(self.knotters_status)
self.thrd.finish.connect(self.finished)
self.thrd.ex.connect(self.thrd.quit)
self.thrd.finished.connect(self.thrd.deleteLater)
self.thrd.finished.connect(self.stop)
self.thrd.start()
我正在编写 python gui 应用程序来处理图像并从串行端口发送图像颜色并显示结果,但不幸的是我的 gui 冻结了。我尝试使用 QApllication.processEvents 并且它可以工作但是我的程序速度很慢而且速度对我来说非常重要并且每一秒一次迭代应该完成然后我使用 QThread 并且我的应用程序仍然冻结。这是我的代码:
class Worker(QObject):
progress = pyqtSignal(int)
gui_update = pyqtSignal()
def __init__(self, knot, lay, baft):
super().__init__()
self.knot = knot
self.lay = lay
self.baft = baft
def run(self):
while self.knot <= knotter_width:
color_to_send = []
for i in range(1, number_of_knotter + 1):
color_to_send.append(self.baft["knotters"][i][self.lay][self.knot])
self.progress.emit(self.knot)
self.gui_update.emit() # for updating gui but not work
QThread 设置:
self.thrd = QThread()
self.worker = Worker(self.knot, self.lay, self.baft)
self.worker.moveToThread(self.thrd)
self.thrd.started.connect(self.worker.run)
self.worker.progress.connect(self.progress)
self.worker.gui_update.connect(self.knotters_status)
self.worker.finish.connect(self.finished)
self.worker.ex.connect(self.thrd.quit)
self.worker.ex.connect(self.worker.deleteLater)
self.thrd.finished.connect(self.thrd.deleteLater)
self.thrd.start()
经过三天的研究,我发现在设置线程后,您需要在创建线程对象后立即调用 processEvents(),在这种情况下,它不会导致速度变慢每件事都很完美。 注意:您应该将参数放在 init 函数中。
这是我的最终代码:
class Thrd(QThread):
progress = pyqtSignal(int)
gui_update = pyqtSignal()
finish = pyqtSignal(bool)
ex = pyqtSignal()
def __init__(self, knot, lay, baft):
super().__init__()
self.knot = knot
self.lay = lay
self.baft = baft
def run(self):
while condition:
# some process
self.progress.emit(self.knot)
self.gui_update.emit()
time.sleep(0.05) # give a little time to update gui
self.finish.emit(True)
self.ex.emit()
和设置部分:
self.thrd = Thrd(self.knot, self.lay, self.baft)
app.processEvents()
self.thrd.progress.connect(self.progress)
self.thrd.gui_update.connect(self.knotters_status)
self.thrd.finish.connect(self.finished)
self.thrd.ex.connect(self.thrd.quit)
self.thrd.finished.connect(self.thrd.deleteLater)
self.thrd.finished.connect(self.stop)
self.thrd.start()