![](/img/trans.png)
[英]How to reduce the space between Two Widgets in PyQt5 QGridLayout?
[英]How to switch between two PyQt5 MainWindow widgets
我正在編寫一個包含兩個不同部分的程序-我們將其稱為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()
您可以使用信號和插槽來分隔邏輯,而不是在類之間共享全局參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.