PyQt5 - How to emit signal from worker tread to call event by GUI thread(PyQt5 - 如何从工作线程发出信号以通过 GUI 线程调用事件)
问题描述
正如我在标题中提到的.我该怎么做?
As I Mentioned in Title. How can i do something like this?
class Main(QWidget):
def __init__(self):
super().__init__()
def StartButtonEvent(self):
self.test = ExecuteThread()
self.test.start()
def MyEvent(self):
#MainThreadGUI
class ExecuteThread(QThread):
def run(self):
# A lot of work
# Signal to main thread about finishing of job = mainthread will perform MyEvent
我在这里找到了一些教程 pyqt4 在线程到主线程中的插槽
I found some tutorials here pyqt4 emiting signals in threads to slots in main thread
这里 从 pyQt Qthread 发出信号
但它似乎在 PyQt5 中不起作用:/
But it seems it does not working in PyQt5 :/
推荐答案
只需使用 QThread.finished 信号在这里.如果您完成线程,它将自动执行.当然你也可以根据需要定义自己的自定义信号.
Just use the QThread.finished signal here. It will be executed automatically if you finish your thread. Of course you can also define your own custom signal if you want.
from PyQt5.QtCore import pyqtSignal
class Main(QWidget):
def __init__(self):
super().__init__()
def StartButtonEvent(self):
self.test = ExecuteThread()
self.test.start()
self.test.finished.connect(thread_finished)
self.test.my_signal.connect(my_event)
def thread_finished(self):
# gets executed if thread finished
pass
def my_event(self):
# gets executed on my_signal
pass
class ExecuteThread(QThread):
my_signal = pyqtSignal()
def run(self):
# do something here
self.my_signal.emit()
pass
这篇关于PyQt5 - 如何从工作线程发出信号以通过 GUI 线程调用事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PyQt5 - 如何从工作线程发出信号以通过 GUI 线程调用事件
基础教程推荐
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
