Python 使用多个文件构建GUI,如何在子GUI文件之间传输信号?

Python 使用多个文件构建GUI,如何在子GUI文件之间传输信号?,python,pyqt,pyqt5,Python,Pyqt,Pyqt5,我的项目看起来像: project |___ proj.py |___ gui |___ guiMain.py |___ guiPart01.py |___ guiPart02.py |___ guiPart03.py |___ guiPart04.py |___ otherModels 因为我有一个复杂的GUI,所以我想将GUI划分为几个子

我的项目看起来像:

project
    |___ proj.py
    |___ gui
            |___ guiMain.py 
            |___ guiPart01.py 
            |___ guiPart02.py 
            |___ guiPart03.py 
            |___ guiPart04.py 
    |___ otherModels
因为我有一个复杂的GUI,所以我想将GUI划分为几个子部分。但子GUI文件之间必须有信号和插槽。我的问题是,我不知道如何在子GUI之间传输信号

我在下面张贴代码

对于我的问题:从
guiPart2.py
,我想直接更改
guiPart1.py
中的lineEdit,如果我要更改
guiMain.py
中的代码,我不愿意

guiMain.py

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

from gui.guiPart1 import GUIPart1
from gui.guiPart2 import GUIPart2

class GUIMain(QMainWindow):
    def __init__(self):
        super(GUIMain, self).__init__()
        self.init()

    def init(self):
        self.lytMain = QVBoxLayout(self)
        self.lytMain.addLayout(GUIPart1.createLayout_Part1(self))
        self.lytMain.addLayout(GUIPart2.createLayout_Part2(self))

        self.wgtMain = QWidget(self)
        self.wgtMain.setLayout(self.lytMain)

        self.setCentralWidget(self.wgtMain)
guiPart1.py

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

class GUIPart1(QWidget):
    def __init__(self):
        super(GUIPart1, self).__init__()
        self.createLayout_Part1()

    def createLayout_Part1(self):
        self.lineEdit_Part1 = QLineEdit("Part1", self)

        self.lytPart1 = QVBoxLayout(self)
        self.lytPart1.addWidget(self.lineEdit_Part1)
        return self.lytPart1 
guiPart2.py

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

class GUIPart2(QWidget):
    def __init__(self):
        super(GUIPart2, self).__init__()
        self.createLayout_Part2()

    def createLayout_Part2(self):
        self.btnPart2 = QPushButton("Control the lineEdit_Part1", self)
        self.btnPart2.clicked.connect(self.signalChange)

        self.lytPart2 = QVBoxLayout(self)
        self.lytPart2.addWidget(self.btnPart2)
        return self.lytPart2 

    def signalChange(self):
        # hier I have problem.
        # I want to change the text in lineEdit_Part1 by adding a string "button clicked " into it, if the btnPart2 is everytime clicked.
        # Besides, I want to have the guiMain.py as clean as possible. I would not perfer if the signals are through the guiMain.py, but direct from this guiPart2.py into guiPart1.py.
        pass

感谢您的任何帮助。谢谢。

我注意到了需要注意的行。试试看:

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

#from gui.guiPart1 import GUIPart1
class GUIPart1(QWidget):
#    def __init__(self):
#        super(GUIPart1, self).__init__()
    def __init__(self, parent=None):                              # ? colors,  + parent    
        super(GUIPart1, self).__init__(parent)                    #  + parent       
        self.createLayout_Part1()

    def createLayout_Part1(self):
        self.lineEdit_Part1 = QLineEdit("Part1", self)
        self.lytPart1 = QVBoxLayout(self)
        self.lytPart1.addWidget(self.lineEdit_Part1)
#        return self.lytPart1 


#from gui.guiPart2 import GUIPart2
class GUIPart2(QWidget):
    def __init__(self, parent=None):                               # ? colors,   + parent  
        super(GUIPart2, self).__init__(parent)                     #  + parent 

        self.parent = parent                                       # +++
        self.createLayout_Part2()

    def createLayout_Part2(self):
        self.btnPart2 = QPushButton("Control the lineEdit_Part1", self)
        self.btnPart2.clicked.connect(self.signalChange)

        self.lytPart2 = QVBoxLayout(self)
        self.lytPart2.addWidget(self.btnPart2)
#        return self.lytPart2 

    def signalChange(self):
#### vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
        text = self.parent.guiPart1.lineEdit_Part1.text()
        self.parent.guiPart1.lineEdit_Part1.setText("{} + {} ".format(text, "button clicked"))
#### ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


class GUIMain(QMainWindow):
    def __init__(self):
        super(GUIMain, self).__init__()
        self.init()

    def init(self):

        centralWidget = QWidget()                                    # +++
        self.setCentralWidget(centralWidget)                         # +++

        self.guiPart1 = GUIPart1(self)
        self.guiPart2 = GUIPart2(self)

        self.lytMain = QVBoxLayout(centralWidget)                    # centralWidget  !
#        self.lytMain.addLayout(GUIPart1.createLayout_Part1(self))
#        self.lytMain.addLayout(GUIPart2.createLayout_Part2(self))
        self.lytMain.addWidget(self.guiPart1)                        # addWidget 
        self.lytMain.addWidget(self.guiPart2)                        # addWidget

        self.wgtMain = QWidget(self)
        self.wgtMain.setLayout(self.lytMain)

        self.setCentralWidget(self.wgtMain)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = GUIMain()
    w.show()
    sys.exit(app.exec_())