首页 新闻 会员 周边 捐助

Pyqt5 怎样实现点击按钮隐藏显示控件?

0
悬赏园豆:20 [待解决问题]

如下,我要实现打开工具时不显示控件plainTextEdit ,然后点击pushubutton_1时控件显示出来,再点击pushbutton_2,控件就隐藏。我之前用了self.plainTextEdit.setVisible(False)去实现,但是有个问题就是,我点击pushubutton_1控件显示出来后,再点击pushbutton_2,控件还是显示状态。此处应该怎样实现呢? 谢谢了!

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

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(405, 194)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
        self.plainTextEdit.setGeometry(QtCore.QRect(30, 20, 331, 51))
        self.plainTextEdit.setObjectName("plainTextEdit")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(30, 90, 81, 23))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(150, 90, 91, 23))
        self.pushButton_2.setObjectName("pushButton_2")
        MainWindow.setCentralWidget(self.centralwidget)
        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.pushButton.setText(_translate("MainWindow", "PushButton_1"))
        self.pushButton_2.setText(_translate("MainWindow", "PushButton_2"))

class TestGUI(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWindow = TestGUI()
    mainWindow.show()
    sys.exit(app.exec_())

 

 
liyangzbx的主页 liyangzbx | 初学一级 | 园豆:74
提问于:2021-04-12 10:18
< >
分享
所有回答(1)
0
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(405, 194)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
        self.plainTextEdit.setGeometry(QtCore.QRect(30, 20, 331, 51))
        self.plainTextEdit.setObjectName("plainTextEdit")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(30, 90, 81, 23))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setGeometry(QtCore.QRect(150, 90, 91, 23))
        self.pushButton_2.setObjectName("pushButton_2")
        MainWindow.setCentralWidget(self.centralwidget)
        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.plainTextEdit.setVisible(False)
        self.pushButton.setText(_translate("MainWindow", "PushButton_1"))
        self.pushButton_2.setText(_translate("MainWindow", "PushButton_2"))
        self.pushButton.clicked.connect(self.btn_1_event)
        self.pushButton_2.clicked.connect(self.btn_2_event)

    def btn_1_event(self):
        self.plainTextEdit.setVisible(True)

    def btn_2_event(self):
        self.plainTextEdit.setVisible(False)


class TestGUI(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainWindow = TestGUI()
    mainWindow.show()
    sys.exit(app.exec_())

你试试

临冬城城主 | 园豆:261 (菜鸟二级) | 2021-04-22 12:04

可以的。谢谢!另外我想请教下,如果我这这里假如说有50个button,除了button_1点击显示这个输入框控件,其它点击全部隐藏,怎样把剩下的49个button绑定到一个函数呢?要写49个connect的话虽然可以,但是不是太麻烦了点?

支持(0) 反对(0) liyangzbx | 园豆:74 (初学一级) | 2021-04-27 11:32

@liyangzbx: for循环绑定同一个函数啊,哈哈哈

支持(0) 反对(0) 临冬城城主 | 园豆:261 (菜鸟二级) | 2021-04-27 11:50

@临冬城城主: 这个button不是有个下标吗,button_2,button_3....这要怎样循环?

支持(0) 反对(0) liyangzbx | 园豆:74 (初学一级) | 2021-04-27 14:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册