python - 使用多个文件构建 GUI,如何在子 GUI 文件之间传输信号?
问题描述
我的项目看起来像:
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_())
推荐阅读
- mysql - mysql 性能超过 1 亿条记录
- nix - 如何使用 Nix niv 引用 github 项目?
- sql-server - 创建用于在其他 PC 上安装 #vb.net 应用程序的设置
- r - 从一个数据帧中提取特定的匹配信息并添加到另一个数据帧
- visual-studio-test-runner - TFS 2018 取消 Visual Studio 测试锁定 dll
- c# - Net Core:使用 InMemory 数据库覆盖 WebApplicationFactory Services DbContext
- javascript - 如何调整 iFrame 大小并保持纵横比
- scala - Scala with Cats 书中这段代码示例中额外的 _ 是什么?
- python - 你如何解决:“AttributeError:'SpriteSheet'对象没有属性'set_colorkey'”?
- javascript - Javascript 从 xmlhttprequest 中删除 blob