我的项目看起来像:
project
|___ proj.py
|___ gui
|___ guiMain.py
|___ guiPart01.py
|___ guiPart02.py
|___ guiPart03.py
|___ guiPart04.py
|___ otherModels
因为我有一个复杂的GUI,所以我想将GUIMain分成几个子部分。但是子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_())