PyQt5信号通信错误

时间:2016-08-12 10:01:25

标签: python python-3.x signals-slots pyqt5 qmdiarea

我需要你帮助解决我遇到的以下问题。 我有两个Python文件,Main.py和Module.py,需要使用PyQt5信号进行通信。这是代码:

Main.py

&(*pVm)

Module.py

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

from MyGUI import main_window_GUI

from Modules import Module.py

class MainWindow(QMainWindow, main_window_GUI.Ui_main_window):
    def __init__(self):
        QMainWindow.__init__(self)
        main_window_GUI.Ui_main_window.__init__(self)
        self.setupUI(self)

        sub_win = QMdiSubWindow()
        sub_win.setWidget(Module.moduleWindow())
        self.mdi.addSubWindow(sub_win)

        # this part reports error saying:
        # 'PyQt5.QtCore.pyqtSignal' object has no attribute 'connect'
        Module.moduleWindow.my_signal.connect(self.do_something)

    def do_something(self):
        pass

任何形式的帮助都非常受欢迎。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您需要连接来自moduleWindow类实例的信号,而不是来自类本身:

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

from MyGUI import main_window_GUI

from Modules import Module

class MainWindow(QMainWindow, main_window_GUI.Ui_main_window):
    def __init__(self):
        QMainWindow.__init__(self)
        main_window_GUI.Ui_main_window.__init__(self)
        self.setupUI(self)

        sub_win = QMdiSubWindow()
        module_window = Module.moduleWindow()
        sub_win.setWidget(module_window)
        self.mdi.addSubWindow(sub_win)

        module_window.my_signal.connect(self.do_something)

    @pyqtSlot()
    def do_something(self):
        pass

我还建议使用documentation

中报告的pyqtSlot装饰do_something方法