首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PyQt5信号通信误差

PyQt5信号通信误差
EN

Stack Overflow用户
提问于 2016-08-12 10:01:24
回答 1查看 264关注 0票数 1

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

Main.py

代码语言:javascript
复制
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

Module.py

代码语言:javascript
复制
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

from MyGUI import module_window_GUI

class moduleWindow(QMainWindow, module_window_GUI.Ui_module_window):
    my_signal = pyqtSignal()
    def __init__(self):
        QMainWindow.__init__(self)
        module_window_GUI.Ui_module_window.__init__(self)
        self.setupUI(self)

        # the rest is not important

    # what is important is th following function
    def closeEvent(self, event):
        # when the user closes this subwindow signal needs to
        # be emitted so that the MainWindow class knows that
        # it's been closed.
        self.my_signal.emit()
        event.accept()

任何帮助都是受欢迎的。提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2016-08-12 10:06:03

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

代码语言:javascript
复制
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

我还建议使用do_something方法来修饰pyqtSlot,如文档中所报告的那样

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38915213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档