This article is mainly for you to introduce the PYTHON3+PYQT5+QT designer to implement the stacking window parts, with a certain reference value, interested in small partners can refer to
This article is a stack window example of the 9th chapter of the Python Qt GUI Quick programming vehicle rental is rewritten with PYTHON3+PYQT5+QT designer.
The first part of the QT Designer is not borrowed, fully implemented in code.
The second part uses QT Designer to realize it quickly.
The first part:
Import sysfrom pyqt5.qtcore Import (Qt) from pyqt5.qtwidgets import (Qapplication, Qcombobox, Qdialog, Qdialogbuttonbox, Q Frame, Qgridlayout, Qhboxlayout, Qlabel, Qspinbox, Qstackedwidget, Qvboxlayout, Qwidget) class Vehiclerentaldlg (QDialog ): Def __init__ (self, Parent=none): Super (Vehiclerentaldlg, self). __init__ (parent) Vehiclelabel = Qlabel ("&vehicle Type: ") Self.vehiclecombobox = Qcombobox () vehiclelabel.setbuddy (Self.vehiclecombobox) Self.vehicleComboBox.addItems (["Car", "Van"]) Colorlabel = Qlabel ("Co&lor:") Self.colorcombobox = Qcombobox () colo Rlabel.setbuddy (Self.colorcombobox) Self.colorComboBox.addItems (["Black", "Blue", "Green", "Red", "Silver", "Whi Te "," Yellow "]) Seatslabel = Qlabel (" &seats: ") Self.seatsspinbox = Qspinbox () Seatslabel.setbuddy (Self.seatsspinbo x) Self.seatsSpinBox.setRange (2,) Self.seatsSpinBox.setValue (4) self.seatsSpinBox.setAlignment (qt.alignright| Qt.alignvcenter) Weightlabel = Qlabel ("&weight:") self.weightspInBox = Qspinbox () weightlabel.setbuddy (Self.weightspinbox) self.weightSpinBox.setRange (1, 8) Self.weightSpinBox.setValue (1) self.weightSpinBox.setAlignment (qt.alignright| Qt.alignvcenter) Self.weightSpinBox.setSuffix ("tons") Volumelabel = Qlabel ("Volu&me") Self.volumespinbox = Qspinb Ox () Volumelabel.setbuddy (Self.volumespinbox) Self.volumeSpinBox.setRange (4,) Self.volumeSpinBox.setValue (10) Self.volumeSpinBox.setAlignment (qt.alignright| Qt.alignvcenter) Self.volumeSpinBox.setSuffix ("cu m") Mileagelabel = Qlabel ("Max. Mileage ") Self.mileagelabel = Qlabel (" "") Self.mileageLabel.setAlignment (qt.alignright| Qt.alignvcenter) Self.mileageLabel.setFrameStyle (qframe.styledpanel| Qframe.sunken) Self.buttonbox = Qdialogbuttonbox (qdialogbuttonbox.ok| Qdialogbuttonbox.cancel) Self.stackedwidget = Qstackedwidget () Carwidget = Qwidget () carlayout = QGridLayout () carLayo Ut.addwidget (colorlabel, 0, 0) carlayout.addwidget (self.colorcombobox, 0, 1) carlayoUt.addwidget (Seatslabel, 1, 0) carlayout.addwidget (self.seatsspinbox, 1, 1) carwidget.setlayout (carLayout) Self.stackedWidget.addWidget (carwidget) Vanwidget = Qwidget () vanlayout = Qgridlayout () vanlayout.addwidget ( Weightlabel, 0, 0) vanlayout.addwidget (self.weightspinbox, 0, 1) vanlayout.addwidget (Volumelabel, 1, 0) vanLayout.addWi Dget (Self.volumespinbox, 1, 1) vanwidget.setlayout (vanlayout) self.stackedWidget.addWidget (vanwidget) toplayout = Qhboxlayout () toplayout.addwidget (Vehiclelabel) toplayout.addwidget (self.vehiclecombobox) bottomLayout = Qhboxlayout () bottomlayout.addwidget (Mileagelabel) bottomlayout.addwidget (self.mileagelabel) layout = QVBoxLayout () Layout.addlayout (toplayout) layout.addwidget (self.stackedwidget) layout.addlayout (bottomLayout) Layout.addWidget ( Self.buttonbox) self.setlayout (layout) self.buttonBox.accepted.connect (self.accept) Self.buttonBox.rejected.connect (Self.reject) self.vehiclecombobox.currentindexchanged[str].connect (SELF.SETWIdgetstack) Self.weightspinbox.valuechanged[int].connect (self.weightchanged) self.setwindowtitle ("Vehicle Rental" ) def setwidgetstack (self, text): if text = = "Car": Self.stackedWidget.setCurrentIndex (0) Self.mileageLabel.setText (" + Miles ") else:self.stackedWidget.setCurrentIndex (1) self.weightchanged (Self.weightSpinBox.value ()) def Weightch Anged (self, amount): Self.mileageLabel.setText ("{0} Miles". Format (8000/amount)) app = Qapplication (sys.argv) Form = Vehiclerentaldlg () form.show () app.exec_ ()
Part II:
/home/yrd/eric_workspace/vehicle/ui_vehiclerentaldlg.py
#-*-Coding:utf-8-*-# Form Implementation generated from reading UI file '/home/yrd/eric_workspace/vehicle/vehiclerenta Ldlg.ui ' # Created by:pyqt5 UI code generator 5.7## warning! All changes made in this file would be Lost!from PyQt5 import Qtcore, Qtgui, Qtwidgetsclass Ui_vehiclerentaldlg (object): De F Setupui (Self, Vehiclerentaldlg): Vehiclerentaldlg.setobjectname ("Vehiclerentaldlg") vehiclerentaldlg.resize (206, 246) Self.gridlayout = Qtwidgets.qgridlayout (Vehiclerentaldlg) self.gridlayout.setContentsMargins (9, 9, 9, 9) Self.gri Dlayout.setspacing (6) self.gridlayout.setObjectName ("GridLayout") Self.buttonbox = Qtwidgets.qdialogbuttonbox ( VEHICLERENTALDLG) self.buttonBox.setOrientation (QtCore.Qt.Horizontal) self.buttonBox.setStandardButtons ( qtwidgets.qdialogbuttonbox.cancel| QTWIDGETS.QDIALOGBUTTONBOX.OK) self.buttonBox.setObjectName ("Buttonbox") Self.gridlayout.addWidget ( Self.buttonbox, 4, 0, 1, 1) Spaceritem = Qtwidgets.qspaceritem (188,, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) Self.gridlayout.addItem (Spaceritem, 3, 0, 1, 1) self.hboxlayout = Qtwidgets.qhboxlayo UT () self.hboxlayout.setContentsMargins (0, 0, 0, 0) self.hboxlayout.setSpacing (6) Self.hboxlayout.setObjectName (" Hboxlayout ") Self.label_6 = Qtwidgets.qlabel (Vehiclerentaldlg) self.label_6.setObjectName (" Label_6 ") Self.hboxlayout.addWidget (self.label_6) Self.mileagelabel = Qtwidgets.qlabel (Vehiclerentaldlg) Self.mileageLabel.setFrameShape (QtWidgets.QFrame.StyledPanel) Self.mileageLabel.setFrameShadow ( QtWidgets.QFrame.Sunken) self.mileageLabel.setAlignment (qtcore.qt.alignright| Qtcore.qt.aligntrailing| QtCore.Qt.AlignVCenter) self.mileageLabel.setObjectName ("Mileagelabel") Self.hboxlayout.addWidget ( Self.mileagelabel) Self.gridlayout.addLayout (self.hboxlayout, 2, 0, 1, 1) Self.stackedwidget = Qtwidgets.qstackedwidget (VEHICLERENTALDLG) self.stackedWidget.setObjectName ("Stackedwidget") self.page_2 = Qtwidgets.qwidget () self.page_2.setObjectName ("page_2")SELF.GRIDLAYOUT1 = Qtwidgets.qgridlayout (self.page_2) self.gridlayout1.setContentsMargins (9, 9, 9, 9) Self.gridlayout1.setSpacing (6) self.gridlayout1.setObjectName ("gridlayout1") Self.colorcombobox = Qtwidgets.qcombobox (self.page_2) self.colorComboBox.setObjectName ("Colorcombobox") Self.colorComboBox.addItem ("" ) Self.colorComboBox.addItem ("") Self.colorComboBox.addItem ("") Self.colorComboBox.addItem ("") Self.colorComboBox.addItem ("") Self.colorComboBox.addItem ("") Self.colorComboBox.addItem ("") Self.gridlayout1.addWidget (Self.colorcombobox, 0, 1, 1, 1) self.label_4 = Qtwidgets.qlabel (self.page_2) self.label_4. Setobjectname ("Label_4") self.gridlayout1.addWidget (self.label_4, 0, 0, 1, 1) self.label_5 = Qtwidgets.qlabel ( self.page_2) self.label_5.setObjectName ("Label_5") Self.gridlayout1.addWidget (self.label_5, 1, 0, 1, 1) Self.seatsspinbox = Qtwidgets.qspinbox (self.page_2) self.seatsSpinBox.setAlignment (QtCore.Qt.AlignRight) Self.seatsSpinBox.setMinimum (2) self.seatSspinbox.setmaximum (Self.seatsSpinBox.setProperty) ("value", 4) self.seatsSpinBox.setObjectName ("Seatsspinbox") Self.gridlayout1.addWidget (Self.seatsspinbox, 1, 1, 1, 1) self.stackedWidget.addWidget (self.page_2) Self.page = Qtwidgets.qwidget () self.page.setObjectName ("page") Self.gridlayout2 = Qtwidgets.qgridlayout (self.page) Self.gridlayout2.setContentsMargins (9, 9, 9, 9) self.gridlayout2.setSpacing (6) Self.gridlayout2.setObjectName (" Gridlayout2 ") Self.weightspinbox = Qtwidgets.qspinbox (self.page) self.weightSpinBox.setAlignment ( QtCore.Qt.AlignRight) Self.weightSpinBox.setMinimum (1) self.weightSpinBox.setMaximum (8) Self.weightSpinBox.setObjectName ("Weightspinbox") self.gridlayout2.addWidget (Self.weightspinbox, 0, 1, 1, 1) Self.label_3 = Qtwidgets.qlabel (self.page) self.label_3.setObjectName ("Label_3") Self.gridlayout2.addWidget ( Self.label_3, 1, 0, 1, 1) self.label_2 = Qtwidgets.qlabel (self.page) self.label_2.setObjectName ("label_2") self.gridlay Out2.addwidget (Self.label_2, 0, 0, 1, 1) Self.volumespinbox = Qtwidgets.qspinbox (self.page) self.volumeSpinBox.setAlignment ( QtCore.Qt.AlignRight) Self.volumeSpinBox.setMinimum (4) self.volumeSpinBox.setMaximum (22) Self.volumeSpinBox.setProperty ("value", ten) Self.volumeSpinBox.setObjectName ("Volumespinbox") Self.gridlayout2.addWidget (Self.volumespinbox, 1, 1, 1, 1) self.stackedWidget.addWidget (self.page) Self.gridlayout.addWidget (self.stackedwidget, 1, 0, 1, 1) self.hboxlayout1 = Qtwidgets.qhboxlayout () Self.hboxlayout1.setContentsMargins (0, 0, 0, 0) self.hboxlayout1.setSpacing (6) Self.hboxlayout1.setObjectName (" Hboxlayout1 ") Self.label = Qtwidgets.qlabel (Vehiclerentaldlg) self.label.setObjectName (" label ") Self.hboxlayout1.addWidget (self.label) Self.vehiclecombobox = Qtwidgets.qcombobox (Vehiclerentaldlg) Self.vehicleComboBox.setObjectName ("Vehiclecombobox") Self.vehicleComboBox.addItem ("") Self.vehicleComboBox.addItem ("") Self.hboxlayout1.addWidget (Self.vehiclecombobox) Self.gridlaYout.addlayout (self.hboxlayout1, 0, 0, 1, 1) self.label_4.setBuddy (Self.colorcombobox) Self.label_5.setBuddy ( Self.seatsspinbox) Self.label_3.setBuddy (Self.volumespinbox) Self.label_2.setBuddy (Self.seatsspinbox) Self.label.setBuddy (Self.vehiclecombobox) Self.retranslateui (VEHICLERENTALDLG) Self.stackedWidget.setCurrentIndex (0) self.vehiclecombobox.currentindexchanged[' int '].connect ( Self.stackedWidget.setCurrentIndex) Self.buttonBox.accepted.connect (vehiclerentaldlg.accept) Self.buttonBox.rejected.connect (Vehiclerentaldlg.reject) QtCore.QMetaObject.connectSlotsByName (Vehiclerentaldlg ) def retranslateui (self, vehiclerentaldlg): _translate = QtCore.QCoreApplication.translate Vehiclerentaldlg.setwindowtitle (_translate ("Vehiclerentaldlg", "Vehicle Rental")) Self.label_6.setText (_translate ("Vehiclerentaldlg", "Max." Mileage: ") Self.mileageLabel.setText (_translate (" Vehiclerentaldlg "," "") Self.colorComboBox.setItemText (0, _translate ("Vehiclerentaldlg", "Black")) Self. Colorcombobox.setitemtext (1, _translate ("Vehiclerentaldlg", "Blue")) Self.colorComboBox.setItemText (2, _translate ("Vehiclerentaldlg", "Green")) Self.colorComboBox.setItemText (3, _translate ("Vehiclerentaldlg", "Red")) Self.colorComboBox.setItemText (4, _ Translate ("Vehiclerentaldlg", "Silver")) Self.colorComboBox.setItemText (5, _translate ("Vehiclerentaldlg", "white") ) Self.colorComboBox.setItemText (6, _translate ("Vehiclerentaldlg", "Yellow")) Self.label_4.setText (_translate (" Vehiclerentaldlg "," Co&lor: ")) Self.label_5.setText (_translate (" Vehiclerentaldlg "," &seats: ")) Self.weightSpinBox.setSuffix (_translate ("Vehiclerentaldlg", "tons")) Self.label_3.setText (_translate (" Vehiclerentaldlg "," Volu&me: ")) Self.label_2.setText (_translate (" Vehiclerentaldlg "," &weight: ")) Self.volumeSpinBox.setSuffix (_translate ("Vehiclerentaldlg", "cu m") Self.label.setText (_translate (" Vehiclerentaldlg "," &vehicle Type: ")) Self.vehicleComboBox.setItemText (0, _translate (" VehicleRentaldlg "," Car ")) Self.vehicleComboBox.setItemText (1, _translate (" Vehiclerentaldlg "," Van "))
/home/yrd/eric_workspace/vehicle/vehiclerentaldlg.py
#-*-coding:utf-8-*-"" "Module implementing Vehiclerentaldlg." "" Import sysfrom pyqt5.qtcore import pyqtslotfrom pyqt5.qtwidgets import Qdialog,qapplicationfrom Ui_vehiclerentaldlg Import Ui_vehiclerentaldlgclass Vehiclerentaldlg (Qdialog, Ui_vehiclerentaldlg): "" "Class documentation goes here." " def __init__ (self, Parent=none): "" "Constructor @param parent reference to the parent widget @type qwidget" "Supe R (Vehiclerentaldlg, self). __init__ (parent) self.setupui (self) Self.vehicleComboBox.setFocus () @pyqtSlot (int) def On_ Weightspinbox_valuechanged (self, amount): Self.mileageLabel.setText ("{0} Miles". Format (8000/amount)) @pyqtSlot (str ) def on_vehiclecombobox_currentindexchanged (self, text): if text = = "Car": Self.mileageLabel.setText ("" ") El Se:self.on_weightSpinBox_valueChanged (Self.weightSpinBox.value ()) If __name__ = = "__main__": App = Qapplication (sy S.ARGV) Form = Vehiclerentaldlg () form.show () app.exec_ ()
Operation Result: