如何通过按下 Pyqt5 或 PyQt4 工具栏中的按钮来设置表单的显示

Posted

技术标签:

【中文标题】如何通过按下 Pyqt5 或 PyQt4 工具栏中的按钮来设置表单的显示【英文标题】:how to set the display of the form by pressing the button from the toolbar in Pyqt5 or PyQt4 【发布时间】:2018-04-10 09:59:13 【问题描述】:

例如,当我打开应用程序向我显示一个表单时,当我单击按钮时显示第二个表单,其中将包含数据输入字段

首页

第二种形式

【问题讨论】:

【参考方案1】:

有几种方法可以实现您想要的。最简单(不是最干净)的是将“表单”中的小部件分组到容器(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_()

【讨论】:

以上是关于如何通过按下 Pyqt5 或 PyQt4 工具栏中的按钮来设置表单的显示的主要内容,如果未能解决你的问题,请参考以下文章

pyqt4 pyqt5 哪个好用

在一个应用程序中同时支持 PyQt4 和 PyQt5 的常用方法是啥?

从 PyQt4 迁移到 PyQt5 时面临的问题

PyQt4 代码在 PyQt5 (QHeaderView) 上不起作用

PyQt4->PyQt5 翻译

PyQt4 到 PyQt5 怎么样?