MainWindow 对象没有属性 'connect'

MainWindow object has no attribute 'connect'

我想知道是否有人可以帮助我解决这个关于 PyQt5 中的插槽连接的问题。以下代码片段将向您展示我的问题所在。

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        path = os.path.join(os.path.dirname(__file__), 'GUI/Main_GUI.ui')
        self.gui = loadUi(path)

        self.gui.button_1.clicked.connect(self.run.this)

    def _connect_my_slots(self, origin):
        self.connect(origin, SIGNAL('completed'), self._show_results)
        self.connect(origin, SIGNAL('error'), self._show_error)

    def run_this(self):
        myThread = LongRunningThing()
        self._connect_my_slots(self.myThread) # THIS IS THE PART THAT CAUSES ERROR

如您所见,我的 MainWindow 对象是我的 UI 文件(来自 QtDesigner 5),一旦我调用 _connect_my_slots 函数,它就会抛出一个错误:

AttributError: 'MainWindow' object has no attribute 'connect'

您正在使用 PyQt5 不再支持的旧式信号和槽。

旧样式:

self.connect(origin, SIGNAL('completed'), self._show_results)

现在应该以新的样式书写:

origin.completed.connect(self._show_results)

有关详细信息,请参阅 New-style Signal and Slot Support 上的文档。