gpt4 book ai didi

python - PyQt5 - 如何从 worker tread 发出信号以通过 GUI 线程调用事件

转载 作者:太空宇宙 更新时间:2023-11-03 14:08:42 24 4
gpt4 key购买 nike

正如我在标题中提到的。我怎样才能做这样的事情?

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 emiting signals in threads to slots in main thread

这里Emit signal from pyQt Qthread

但它似乎在 PyQt5 中不起作用:/

最佳答案

只需使用 QThread.finished信号在这里。如果您完成线程,它将自动执行。当然,如果需要,您也可以定义自己的自定义信号。

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

关于python - PyQt5 - 如何从 worker tread 发出信号以通过 GUI 线程调用事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41184719/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com