【问题标题】:How to hide Qcheckbox when Qcombox box selects an item?如何隐藏组合框中的复选框选择一个项目?
【发布时间】:2019-03-29 07:59:54
【问题描述】:
from PyQt5 import QtCore, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(762, 590)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.checkBox = QtWidgets.QCheckBox('box', self.centralwidget)
        self.checkBox.setGeometry(QtCore.QRect(150, 75, 181, 20))
        self.checkBox.setObjectName("checkBox")


        self.comboBox = QtWidgets.QComboBox(self.centralwidget)
        self.comboBox.setGeometry(QtCore.QRect(150,160,100,20))
        self.comboBox.addItem("Yes")
        self.comboBox.addItem("No")
        self.comboBox.setObjectName("comboBox")



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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "11"))

        MainWindow.show()

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

我创建了一个带有“是”和“否”的组合框,当我在组合框中选择“否”时,我想隐藏复选框,有人可以帮忙吗?

我试图创建一个函数,当 self.comboBox.currentText == "Yes" 时,运行 self.checkBox.hide(),但它不起作用......

【问题讨论】:

    标签: python pyqt pyqt5 qcombobox qcheckbox


    【解决方案1】:

    您必须使用currentTextChanged 信号通知您如果QComboBox 选择已更改,向您发送新文本,那么您应该只将其与文本进行比较,并与setVisible() 方法一起满足您的要求.

        self.comboBox.currentTextChanged.connect(self.handle_current_text_changed)
    
    def handle_current_text_changed(self, text):
        self.checkBox.setVisible(text == "Yes")
    

    【讨论】:

      【解决方案2】:

      使用信号和槽来做到这一点。使用插槽 hideCheckBox 捕获 Combobox 的 editTextChanged 信号。

      comboBox.currentTextChanged.connect(func)

      在函数func 中,当文本为“NO”时简单地将Visibility 设置为false,当文本为“YES”时设置为true。

      【讨论】:

        【解决方案3】:

        如果您想在comboBox 状态为HIDE 时隐藏复选框,并在组合框状态为UNHIDE 时取消隐藏checkBox,请使用IF 构造来捕获组合框。根据状态,将一个或另一个值应用于复选框:

        
        class Ui_MainWindow(object):
            def setupUi(self, MainWindow):
                MainWindow.setObjectName("MainWindow")
                MainWindow.resize(762, 590)
                self.centralwidget = QtWidgets.QWidget(MainWindow)
                self.centralwidget.setObjectName("centralwidget")
        
                self.checkBox = QtWidgets.QCheckBox('box', self.centralwidget)
                self.checkBox.setGeometry(QtCore.QRect(150, 75, 181, 20))
                self.checkBox.setObjectName("checkBox")
        
        
                self.comboBox = QtWidgets.QComboBox(self.centralwidget)
                self.comboBox.setGeometry(QtCore.QRect(150,160,100,20))
                self.comboBox.addItem("UNHIDE")
                self.comboBox.addItem("HIDE")
                self.comboBox.setObjectName("comboBox")
        
        
                MainWindow.setCentralWidget(self.centralwidget)
                self.retranslateUi(MainWindow)
                QtCore.QMetaObject.connectSlotsByName(MainWindow)
        
                self.comboBox.currentTextChanged.connect(self.hidefunction) # code for connect to function below
        
            def hidefunction(self):
                text = str(self.comboBox.currentText()) 
                # this is the state of the current text in combobox. Below simple IF block.
                if text == "UNHIDE":
                    self.checkBox.setHidden(False) 
                # its HIDE - your checkBox when comboBox changed his state 
                else:
                    self.checkBox.setHidden(True) 
                # its HIDE your checkBox when comboBox changed his state 
            
                   
        
            def retranslateUi(self, MainWindow):
                _translate = QtCore.QCoreApplication.translate
                MainWindow.setWindowTitle(_translate("MainWindow", "11"))
        
                MainWindow.show()
        
        if __name__ == "__main__":
            import sys
        
            app = QtWidgets.QApplication(sys.argv)
            MainWindow = QtWidgets.QMainWindow()
            ui = Ui_MainWindow()
            ui.setupUi(MainWindow)
            MainWindow.show()
            sys.exit(app.exec_())
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-02-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多