PyQt5 - 如何从工作线程发出信号以通过 GUI 线程调用事件
- 作者: 嗫?暁雲?
- 来源: 51数据库
- 2022-12-23
问题描述
正如我在标题中提到的.我该怎么做?
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
推荐阅读
热点文章
Discord.py(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0
