我一直试图找到一种方法来从 main 之外的 Python 线程更新 GUI 线程。 PyQt5 docs在 sourceforge 上有关于如何做到这一点的很好的说明。但我仍然无法让事情发挥作用。
有没有一种很好的方法来解释交互式 session 的以下输出?难道不应该有办法在这些对象上调用发射方法吗?
>>> 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'
最佳答案
以下文字和代码在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"
关于user-interface - PyQt5 信号和插槽 'QObject has no attribute' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17578428/