我正在编写一个程序,它有两个不同的部分 - 让我们称它们为 sub1 和 sub2。当我最初运行程序时,会显示 sub1,并且我在后台加载 sub2 但不显示它。我在 sub1 中有一个菜单操作,允许您切换到 sub2,在 sub2 中有一个菜单操作,允许您切换回 sub1。我遇到的问题是当尝试从 sub2 切换回 sub1 时。从 sub1 到 sub2 效果很好; sub1 隐藏,sub2 显示。但是,当尝试再次显示 sub1 时,sub2 不会被隐藏。我对 PyQt 和 Python 都很陌生,所以我还不知道所有复杂的事情。所以,我使用的方法只是我通过反复试验得出的方法,绝不需要这样。下面的简化代码。
#mass module
class MASS(PyQt5.QtWidgets.QMainWindow, massUi.Ui_MainWindow):
def __init__(self):
super(MASS, self).__init__()
self.actionSwitchToCompEval.triggered.connect(self.switchToCompEval)
def switchToCompEval(self):
massForm = main.massForm
massForm.hide()
compForm = main.compForm
compForm.show()
def showMass(self):
main(1)
def main(initiate=None):
if initiate == None:
main.app = PyQt5.QtWidgets.QApplication(sys.argv)
main.massForm = MASS()
main.compForm = CompEval.CompEval()
main.massForm.show()
main.app.exec_()
elif initiate == 1:
main.massForm = MASS()
main.compForm = CompEval.CompEval()
main.compForm.hide()
main.massForm.show()
elif initiate == 2:
pass
if __name__ == '__main__':
main()
#comp module
class CompEval(PyQt5.QtWidgets.QMainWindow, compEvalUi.Ui_MainWindow):
def __init__(self):
super(CompEval, self).__init__()
self.actionSwitchToMASS.triggered.connect(self.switchToMass)
def switchToMass(self):
mass.MASS().showMass()
def main():
form = CompEval()
form.show()
在 switchToCompEval 函数中,引用 main.massForm 和 main.compForm 变量似乎工作正常,但是当我尝试从 sub2(comp) 返回到 sub1(mass) 时,出现错误,表明该函数不包含该变量,这对我来说似乎很奇怪。我知道目前我的设置有几个方面很奇怪并且远非理想,因此任何建议将不胜感激。谢谢。
因此,经过多次实验,我确定解决此问题的最佳方法是将模块组合成一个。如果您有多个 MainWindow 小部件并且需要能够在它们之间来回切换,请将访问所有小部件的类保留在同一模块中。
所以我有两个小部件类:
import PyQt5
import sys
#Below are the modules that are auto-generated when using Qt Designer to make an interface
import compWidget as compEvalUi
import massWidget as massUi
class MASS(PyQt5.QtWidgets.QMainWindow, massUi.Ui_MainWindow
def __init__(self):
super(MASS, self).__init__()
#setupUi function is in the auto-generated module and contains all the attributes of the interface
self.setupUi(self)
#menuSwitch is the name of the button/menu item that would need to be clicked
#in order to switch windows.
#The example shown here is if the action was attached to a menu-dropdown item
#which is why 'triggered' is used as opposed to 'clicked' if it were attached to a button
self.menuSwitch.triggered.connect(self.switchToCompWidget)
def switchToCompWidget(self):
INITIALIZE.massForm.hide()
INITIALIZE.compForm.show()
class CompEval(PyQt5.QtWidgets.QMainWindow, compEvalUi.Ui_MainWindow):
def __init__(self):
super(CompEval, self).__init__()
self.setupUi(self)
self.menuSwitch.triggered.connect(self.switchToMassWidget)
def switchToMassWidget(self):
INITIALIZE.compForm.hide()
INITIALIZE.massForm.show()
class INITIALIZE:
def __init__(self):
app = PyQt5.QtWidgets.QApplication(sys.argv)
INITIALIZE.massForm = MASS()
INITIALIZE.massForm.show()
INITIALIZE.compForm = CompEval()
app.exec_()
def main():
program = INITIALIZE()
if __name__ =='__main__':
main()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)