PyQt5:如何有效地管理多个页面

时间:2018-10-10 12:42:04

标签: python python-3.x pyqt pyqt5 qstackedwidget

我试图弄清楚如何在基于PyQt5桌面的应用程序中有效地管理多个页面。 例如:我有2页1)主页2)订单输入

在MainPage上,单击Order Button->创建Order页面的新实例并显示Order(主窗口)页面+隐藏MainPage

在“订购页面”上,单击“主页”按钮->这应该显示旧的MainPage。 该怎么做?我可以创建新实例并显示,但不能访问以前的实例。

我注意到一个使用QStackWidget的示例,但找不到适合我的示例。

有什么方法可以使用QStackWidget实现示例吗?

main.py

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow

import sys

from view.mainwindow import Ui_MainWindow
from view.ui_op_MainWindow import Ui_op_MainWindow

class Main(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(Main, self).__init__(parent)
        self.setupUi(self)
        self.mp_orderPushButton.clicked.connect(self.openOrderPage)
    def openOrderPage(self):
        self.orderWindow = QtWidgets.QMainWindow()
        self.ui = Ui_op_MainWindow()
        self.ui.setupUi(self.orderWindow)
        mp_window.hide()
        self.orderWindow.show()
if __name__ == '__main__':
    app = QApplication(sys.argv)
    mp_window = Main()
    mp_window.show()
    sys.exit(app.exec_())

** mainWindow.py **

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(594, 421)
        self.centralWidget = QtWidgets.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")
        self.mp_entryFrame = QtWidgets.QFrame(self.centralWidget)
        self.mp_entryFrame.setGeometry(QtCore.QRect(40, 40, 174, 181))
        self.mp_entryFrame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.mp_entryFrame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.mp_entryFrame.setObjectName("mp_entryFrame")
        self.verticalLayoutWidget = QtWidgets.QWidget(self.mp_entryFrame)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(30, 10, 114, 161))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.mp_entryVerticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.mp_entryVerticalLayout.setContentsMargins(11, 11, 11, 11)
        self.mp_entryVerticalLayout.setSpacing(6)
        self.mp_entryVerticalLayout.setObjectName("mp_entryVerticalLayout")
        self.mp_entryLabel = QtWidgets.QLabel(self.verticalLayoutWidget)
        self.mp_entryLabel.setObjectName("mp_entryLabel")
        self.mp_entryVerticalLayout.addWidget(self.mp_entryLabel, 0, QtCore.Qt.AlignHCenter)
        self.mp_orderPushButton = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.mp_orderPushButton.setObjectName("mp_orderPushButton")
        self.mp_entryVerticalLayout.addWidget(self.mp_orderPushButton)
        self.mp_dispatchPushButton = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.mp_dispatchPushButton.setObjectName("mp_dispatchPushButton")
        self.mp_entryVerticalLayout.addWidget(self.mp_dispatchPushButton)
        self.mp_millCashPushButton = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.mp_millCashPushButton.setObjectName("mp_millCashPushButton")
        self.mp_entryVerticalLayout.addWidget(self.mp_millCashPushButton)
        self.mp_pCashPushButton = QtWidgets.QPushButton(self.verticalLayoutWidget)
        self.mp_pCashPushButton.setObjectName("mp_pCashPushButton")
        self.mp_entryVerticalLayout.addWidget(self.mp_pCashPushButton)
        MainWindow.setCentralWidget(self.centralWidget)
        self.menuBar = QtWidgets.QMenuBar(MainWindow)
        self.menuBar.setGeometry(QtCore.QRect(0, 0, 594, 22))
        self.menuBar.setObjectName("menuBar")
        MainWindow.setMenuBar(self.menuBar)
        self.mainToolBar = QtWidgets.QToolBar(MainWindow)
        self.mainToolBar.setObjectName("mainToolBar")
        MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
        self.statusBar = QtWidgets.QStatusBar(MainWindow)
        self.statusBar.setObjectName("statusBar")
        MainWindow.setStatusBar(self.statusBar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.mp_entryLabel.setText(_translate("MainWindow", "Entry"))
        self.mp_orderPushButton.setText(_translate("MainWindow", "Order"))
        self.mp_dispatchPushButton.setText(_translate("MainWindow", "Dispatch"))
        self.mp_millCashPushButton.setText(_translate("MainWindow", "Mill Cash"))
        self.mp_pCashPushButton.setText(_translate("MainWindow", "Party Cash"))

** ui_op_MainWindow **

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_op_MainWindow(object):
    def setupUi(self, op_MainWindow):
        op_MainWindow.setObjectName("op_MainWindow")
        op_MainWindow.resize(696, 444)
        self.op_centralwidget = QtWidgets.QWidget(op_MainWindow)
        self.op_centralwidget.setObjectName("op_centralwidget")
        self.op_homeButton = QtWidgets.QPushButton(self.op_centralwidget)
        self.op_homeButton.setGeometry(QtCore.QRect(570, 10, 114, 32))
        self.op_homeButton.setObjectName("op_homeButton")
        self.formLayoutWidget = QtWidgets.QWidget(self.op_centralwidget)
        self.formLayoutWidget.setGeometry(QtCore.QRect(30, 50, 571, 321))
        self.formLayoutWidget.setObjectName("formLayoutWidget")
        self.op_formLayout = QtWidgets.QFormLayout(self.formLayoutWidget)
        self.op_formLayout.setContentsMargins(0, 0, 0, 0)
        self.op_formLayout.setObjectName("op_formLayout")
        self.op_dateLabel = QtWidgets.QLabel(self.formLayoutWidget)
        self.op_dateLabel.setObjectName("op_dateLabel")
        self.op_formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.op_dateLabel)
        self.op_dateLineEdit = QtWidgets.QLineEdit(self.formLayoutWidget)
        self.op_dateLineEdit.setObjectName("op_dateLineEdit")
        self.op_formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.op_dateLineEdit)
        self.op_millNameLabel = QtWidgets.QLabel(self.formLayoutWidget)
        self.op_millNameLabel.setObjectName("op_millNameLabel")
        self.op_formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.op_millNameLabel)
        self.op_millNameComboBox = QtWidgets.QComboBox(self.formLayoutWidget)
        self.op_millNameComboBox.setObjectName("op_millNameComboBox")
        self.op_millNameComboBox.addItem("")
        self.op_millNameComboBox.addItem("")
        self.op_millNameComboBox.addItem("")
        self.op_formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.op_millNameComboBox)
        self.op_partyNameLabel = QtWidgets.QLabel(self.formLayoutWidget)
        self.op_partyNameLabel.setObjectName("op_partyNameLabel")
        self.op_formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.op_partyNameLabel)
        self.op_partyNameComboBox = QtWidgets.QComboBox(self.formLayoutWidget)
        self.op_partyNameComboBox.setObjectName("op_partyNameComboBox")
        self.op_partyNameComboBox.addItem("")
        self.op_partyNameComboBox.addItem("")
        self.op_formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.op_partyNameComboBox)
        self.op_quantityLabel = QtWidgets.QLabel(self.formLayoutWidget)
        self.op_quantityLabel.setObjectName("op_quantityLabel")
        self.op_formLayout.setWidget(3, QtWidgets.QFormLayout.LabelRole, self.op_quantityLabel)
        self.op_quantityLineEdit = QtWidgets.QLineEdit(self.formLayoutWidget)
        self.op_quantityLineEdit.setObjectName("op_quantityLineEdit")
        self.op_formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.op_quantityLineEdit)
        self.op_rateLabel = QtWidgets.QLabel(self.formLayoutWidget)
        self.op_rateLabel.setObjectName("op_rateLabel")
        self.op_formLayout.setWidget(4, QtWidgets.QFormLayout.LabelRole, self.op_rateLabel)
        self.op_rateLineEdit = QtWidgets.QLineEdit(self.formLayoutWidget)
        self.op_rateLineEdit.setObjectName("op_rateLineEdit")
        self.op_formLayout.setWidget(4, QtWidgets.QFormLayout.FieldRole, self.op_rateLineEdit)
        self.op_etcLabel = QtWidgets.QLabel(self.formLayoutWidget)
        self.op_etcLabel.setObjectName("op_etcLabel")
        self.op_formLayout.setWidget(5, QtWidgets.QFormLayout.LabelRole, self.op_etcLabel)
        self.op_etcLineEdit = QtWidgets.QLineEdit(self.formLayoutWidget)
        self.op_etcLineEdit.setObjectName("op_etcLineEdit")
        self.op_formLayout.setWidget(5, QtWidgets.QFormLayout.FieldRole, self.op_etcLineEdit)
        op_MainWindow.setCentralWidget(self.op_centralwidget)
        self.statusbar = QtWidgets.QStatusBar(op_MainWindow)
        self.statusbar.setObjectName("statusbar")
        op_MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(op_MainWindow)
        QtCore.QMetaObject.connectSlotsByName(op_MainWindow)

        self.op_homeButton.clicked.connect(self.op_homeBtnClicked())

    def retranslateUi(self, op_MainWindow):
        _translate = QtCore.QCoreApplication.translate
        op_MainWindow.setWindowTitle(_translate("op_MainWindow", "MainWindow"))
        self.op_homeButton.setText(_translate("op_MainWindow", "Home"))
        self.op_dateLabel.setText(_translate("op_MainWindow", "Date"))
        self.op_millNameLabel.setText(_translate("op_MainWindow", "Mill Name"))
        self.op_millNameComboBox.setItemText(0, _translate("op_MainWindow", "Select Mill"))
        self.op_millNameComboBox.setItemText(1, _translate("op_MainWindow", "Abc"))
        self.op_millNameComboBox.setItemText(2, _translate("op_MainWindow", "Def"))
        self.op_partyNameLabel.setText(_translate("op_MainWindow", "Party Name"))
        self.op_partyNameComboBox.setItemText(0, _translate("op_MainWindow", "Select Party"))
        self.op_partyNameComboBox.setItemText(1, _translate("op_MainWindow", "ppppppp"))
        self.op_quantityLabel.setText(_translate("op_MainWindow", "Quantity"))
        self.op_rateLabel.setText(_translate("op_MainWindow", "Rate"))
        self.op_etcLabel.setText(_translate("op_MainWindow", "Etc"))

0 个答案:

没有答案