我一直试图找到一种方法来从main之外的Python线程更新GUI线程。 sourceforge上的PyQt5 docs有关于如何执行此操作的良好说明。但我仍然无法让事情发挥作用。
有没有一种好方法可以解释交互式会话中的以下输出?不应该有办法在这些对象上调用emit方法吗?
>>> from PyQt5.QtCore import QObject, pyqtSignal
>>> obj = QObject()
>>> sig = pyqtSignal()
>>> obj.emit(sig)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'QObject' object has no attribute 'emit'
和
>>> obj.sig.emit()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'QObject' object has no attribute 'sig'
和
>>> obj.sig = pyqtSignal()
>>> obj.sig.emit()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'PyQt5.QtCore.pyqtSignal' object has no attribute 'emit'
答案 0 :(得分:19)
以下字词和代码位于PyQt5 docs。
新信号应仅在QObject的子类中定义。必须是类定义的一部分,不能动态添加作为类定义后的类属性。
from PyQt5.QtCore import QObject, pyqtSignal
class Foo(QObject):
# Define a new signal called 'trigger' that has no arguments.
trigger = pyqtSignal()
def connect_and_emit_trigger(self):
# Connect the trigger signal to a slot.
self.trigger.connect(self.handle_trigger)
# Emit the signal.
self.trigger.emit()
def handle_trigger(self):
# Show that the slot has been called.
print "trigger signal received"