关于用户界面:PyQt5信号和插槽“ QObject没有属性”错误

PyQt5 Signals and Slots 'QObject has no attribute' error

我一直在尝试找到一种从main外部的Python线程更新GUI线程的方法。 sourceforge上的PyQt5文档提供了有关如何执行此操作的良好说明。 但是我仍然无法正常工作。

有没有很好的方法来解释交互式会话的以下输出? 难道没有一种方法可以在这些对象上调用emit方法吗?

1
2
3
4
5
6
7
>>> 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'

1
2
3
4
>>> obj.sig.emit()
Traceback (most recent call last):
  File"<stdin>", line 1, in <module>
AttributeError: 'QObject' object has no attribute 'sig'

1
2
3
4
5
>>> 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文档中。

新信号只能在QObject的子类中定义。它们必须是类定义的一部分,并且在定义了类之后不能动态添加为类属性。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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"