簡體   English   中英

如何通過按Pyqt5或PyQt4中的工具欄上的按鈕來設置表單的顯示

[英]how to set the display of the form by pressing the button from the toolbar in Pyqt5 or PyQt4

例如,當我打開應用程序以顯示一個表單時,單擊按鈕顯示第二個表單,該表單將包含數據輸入字段

第二種形式

有幾種方法可以實現您想要的。 最簡單(不是最簡單)的方法是將“表單”中的小部件分組到容器(QFrame,QGroupbox等)中,並將觸發信號從您的操作連接到合適的插槽中,以隱藏/顯示這些容器。 在下面,您可以看到一個示例(PySide),在此示例中,我正在使用兩個QFrame來分隔“表單”。 布局行為可以輕松修改。

表單文件(從設計者的ui文件編譯):

# Form implementation generated from reading ui file 'example.ui'
#
# Created: Tue Apr 10 13:18:07 2018
#      by: pyside-uic 0.2.15 running on PySide 1.2.4
#
# WARNING! All changes made in this file will be lost!

from PySide import QtCore, QtGui

class Ui_Example(object):
    def setupUi(self, Example):
        Example.setObjectName("Example")
        Example.resize(800, 770)
        self.centralwidget = QtGui.QWidget(Example)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtGui.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.fr_one = QtGui.QFrame(self.centralwidget)
        self.fr_one.setFrameShape(QtGui.QFrame.StyledPanel)
        self.fr_one.setFrameShadow(QtGui.QFrame.Raised)
        self.fr_one.setObjectName("fr_one")
        self.verticalLayout_2 = QtGui.QVBoxLayout(self.fr_one)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.bt_quit = QtGui.QPushButton(self.fr_one)
        self.bt_quit.setObjectName("bt_quit")
        self.verticalLayout_2.addWidget(self.bt_quit)
        self.cb_selection = QtGui.QComboBox(self.fr_one)
        self.cb_selection.setObjectName("cb_selection")
        self.verticalLayout_2.addWidget(self.cb_selection)
        spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        self.verticalLayout_2.addItem(spacerItem)
        self.gridLayout.addWidget(self.fr_one, 0, 0, 1, 1)
        self.fr_two = QtGui.QFrame(self.centralwidget)
        self.fr_two.setFrameShape(QtGui.QFrame.StyledPanel)
        self.fr_two.setFrameShadow(QtGui.QFrame.Raised)
        self.fr_two.setObjectName("fr_two")
        self.verticalLayout_5 = QtGui.QVBoxLayout(self.fr_two)
        self.verticalLayout_5.setObjectName("verticalLayout_5")
        self.verticalLayout = QtGui.QVBoxLayout()
        self.verticalLayout.setSpacing(0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.label = QtGui.QLabel(self.fr_two)
        self.label.setObjectName("label")
        self.verticalLayout.addWidget(self.label)
        self.le_fname = QtGui.QLineEdit(self.fr_two)
        self.le_fname.setObjectName("le_fname")
        self.verticalLayout.addWidget(self.le_fname)
        self.verticalLayout_5.addLayout(self.verticalLayout)
        self.verticalLayout_3 = QtGui.QVBoxLayout()
        self.verticalLayout_3.setSpacing(0)
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.label_2 = QtGui.QLabel(self.fr_two)
        self.label_2.setObjectName("label_2")
        self.verticalLayout_3.addWidget(self.label_2)
        self.le_sname = QtGui.QLineEdit(self.fr_two)
        self.le_sname.setObjectName("le_sname")
        self.verticalLayout_3.addWidget(self.le_sname)
        self.verticalLayout_5.addLayout(self.verticalLayout_3)
        self.verticalLayout_4 = QtGui.QVBoxLayout()
        self.verticalLayout_4.setSpacing(0)
        self.verticalLayout_4.setObjectName("verticalLayout_4")
        self.label_3 = QtGui.QLabel(self.fr_two)
        self.label_3.setObjectName("label_3")
        self.verticalLayout_4.addWidget(self.label_3)
        self.le_address = QtGui.QLineEdit(self.fr_two)
        self.le_address.setObjectName("le_address")
        self.verticalLayout_4.addWidget(self.le_address)
        self.verticalLayout_5.addLayout(self.verticalLayout_4)
        spacerItem1 = QtGui.QSpacerItem(20, 267, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        self.verticalLayout_5.addItem(spacerItem1)
        self.gridLayout.addWidget(self.fr_two, 0, 1, 1, 1)
        Example.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(Example)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 47))
        self.menubar.setObjectName("menubar")
        Example.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(Example)
        self.statusbar.setObjectName("statusbar")
        Example.setStatusBar(self.statusbar)
        self.toolBar = QtGui.QToolBar(Example)
        self.toolBar.setObjectName("toolBar")
        Example.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
        self.actionAction1 = QtGui.QAction(Example)
        self.actionAction1.setObjectName("actionAction1")
        self.actionAction2 = QtGui.QAction(Example)
        self.actionAction2.setObjectName("actionAction2")
        self.toolBar.addAction(self.actionAction1)
        self.toolBar.addAction(self.actionAction2)

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

    def retranslateUi(self, Example):
        Example.setWindowTitle(QtGui.QApplication.translate("Example", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.bt_quit.setText(QtGui.QApplication.translate("Example", "Quit", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("Example", "First name", None, QtGui.QApplication.UnicodeUTF8))
        self.label_2.setText(QtGui.QApplication.translate("Example", "Second name", None, QtGui.QApplication.UnicodeUTF8))
        self.label_3.setText(QtGui.QApplication.translate("Example", "Address", None, QtGui.QApplication.UnicodeUTF8))
        self.toolBar.setWindowTitle(QtGui.QApplication.translate("Example", "toolBar", None, QtGui.QApplication.UnicodeUTF8))
        self.actionAction1.setText(QtGui.QApplication.translate("Example", "action1", None, QtGui.QApplication.UnicodeUTF8))
        self.actionAction2.setText(QtGui.QApplication.translate("Example", "action2", None, QtGui.QApplication.UnicodeUTF8))

主文件:

from PySide import QtGui, QtCore
from _example import Ui_Example

class Example(QtGui.QMainWindow):
    def __init__(self, parent = None):
        super(Example, self).__init__(parent)
        self.ui = Ui_Example()
        self.ui.setupUi(self)

        self.ui.fr_two.setVisible(False)
        self.ui.cb_selection.addItem("motif")

        # here are the connections of the corresponding actions on the QToolBar
        self.ui.actionAction1.triggered.connect(self._changeView1)
        self.ui.actionAction2.triggered.connect(self._changeView2)

    def _changeView1(self):
        # fr_one is the first frame (the one that contains the 'quit' button and the QCombobox)
        # fr_two is the second frame (the one that contains the QLineEdits)
        self.ui.fr_one.setVisible(True)
        self.ui.fr_two.setVisible(False)

    def _changeView2(self):
        self.ui.fr_one.setVisible(False)
        self.ui.fr_two.setVisible(True)



if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    app.exec_()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM