pyqt5: 1 button controls the display and hiding of the control

        As shown in the figure below, there is a button in the upper left corner for controlling the display and hiding of the menu bar. The initialization is to display the menu, the first click will hide the menu bar, the second click will display the menu bar, and so on.

         The effect after the menu bar is hidden is shown in the figure below, and the layout is added here. If you don’t know the layout very well, please move to my other article: "Summary of pyqt5 Control Adaptive Window Knowledge Points (Super Detailed Explanation ) , continuously updated...) ":

         Let’s talk about two implementation methods. The first one is the simplest, but the code is relatively long. The specific code is as follows:

        self.pushButton.clicked.connect(self.hideMenu)
        self.stc = 1

    def hideMenu(self):
        if self.stc == 1:
            self.listWidget.hide()
            self.stc = 0

        elif self.stc == 0:
            self.listWidget.show()
            self.stc = 1

        This method is to initialize a value first, and then perform an if loop judgment in the custom function according to this value, and that's it. The second method is as follows: 

self.pushButton.clicked.connect(lambda: self.listWidget.setVisible(not self.listWidget.isVisible()))

        The second method only needs one line of code, which is much simpler than the first method. The core is to use the anonymous function lambda and setVisible function.

        


        The source file code of ui file to py is as follows:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.7
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName("verticalLayout")
        self.widget = QtWidgets.QWidget(self.centralwidget)
        self.widget.setMinimumSize(QtCore.QSize(0, 50))
        self.widget.setMaximumSize(QtCore.QSize(16777215, 50))
        self.widget.setStyleSheet("background-color: rgb(160, 160, 160);")
        self.widget.setObjectName("widget")
        self.pushButton = QtWidgets.QPushButton(self.widget)
        self.pushButton.setGeometry(QtCore.QRect(15, 10, 93, 28))
        self.pushButton.setObjectName("pushButton")
        self.verticalLayout.addWidget(self.widget)
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.listWidget = QtWidgets.QListWidget(self.centralwidget)
        self.listWidget.setMaximumSize(QtCore.QSize(150, 16777215))
        self.listWidget.setObjectName("listWidget")
        self.horizontalLayout.addWidget(self.listWidget)
        self.frame = QtWidgets.QFrame(self.centralwidget)
        self.frame.setStyleSheet("background-color: rgb(204, 204, 204);")
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.horizontalLayout.addWidget(self.frame)
        self.verticalLayout.addLayout(self.horizontalLayout)
        MainWindow.setCentralWidget(self.centralwidget)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "隐藏/显示"))

Guess you like

Origin blog.csdn.net/weixin_53989417/article/details/128514395