【问题标题】:PyQt: make QLabel disappear when certain drop-down value is chosenPyqt:选择某些下拉值的值时使QLabel消失
【发布时间】:2021-03-13 09:50:18
【问题描述】:

我有一个QDialog,其中有几个QLineEdits,还有一个包含3 个条目的下拉列表(QComboBox):通过、通过附加cmets 和失败。

每当用户选择“失败”或“通过额外的 cmets”时,我都会添加一个额外的 QLineEdit 以供评论。如果选择“通过”,则不会添加任何字段。 我设法在通过在开始时的字段中未显示的情况下选择了两个条目之一时,我设法实现了额外的注释字段的一部分,并且只要在输入所选择的条目时,它会变得可见,但我不设法只要选择“通过”,字段就会再次消失。 这里有一些与它有关的sn-ps:

        self.resultLabel = QLabel("Result:", self)
        #define dropdown
        self.resultCombo = QComboBox(self)
        self.resultCombo.addItem("passed")
        self.resultCombo.addItem("passed with additional comments")
        self.resultCombo.addItem("failed")
    
        #define the extra comment label and input field
        self.extraCommentLabel = QLabel("Comment",self)
        self.extraCommentLabel.setVisible(False)
        self.extraCommentLine = QLineEdit(self)
        self.extraCommentLine.setVisible(False)
        self.resultCombo.currentIndexChanged.connect(self.combo_value)
    
        #define layout
        self.resultLayout = QHBoxLayout()
        self.resultLayout.addWidget(self.resultLabel)
        self.resultLayout.addWidget(self.resultCombo)
        self.windowLayout = QVBoxLayout()
        self.windowLayout.addLayout(self.resultLayout)
        self.windowLayout.addLayout(self.extraCommentLayout)
    
     def combo_value(self):
        selected_option = self.resultCombo.currentText()
        print str(selected_option)
        if selected_option is "passed with additional comments" or "failed":
              self.extraCommentLine.setVisible(True)
              self.extraCommentLabel.setVisible(True)
        elif selected_option is "passed":
              self.extraCommentLabel.setVisible(False)

我猜我的def combo_value 丢失了一些东西,但不确定是什么。感谢您的任何提示!

【问题讨论】:

    标签: pyqt qcombobox


    【解决方案1】:

    我认为你错过了self.extraCommentLine.setVisible (False)

    import sys
    from PyQt5.Qt import *
    
    
    class Window(QWidget):
        def __init__(self):
            super().__init__()
     
            self.resultLabel = QLabel("Result:", self)
    
            self.resultCombo = QComboBox(self)
            self.resultCombo.addItem("passed")
            self.resultCombo.addItem("passed with additional comments")
            self.resultCombo.addItem("failed")
            self.resultCombo.currentIndexChanged.connect(self.combo_value)
        
            self.extraCommentLabel = QLabel("Comment", self)
            self.extraCommentLabel.setVisible(False)
            
            self.extraCommentLine = QLineEdit(self)
            self.extraCommentLine.setVisible(False)
    
            self.windowLayout = QGridLayout(self)       
            self.windowLayout.addWidget(self.extraCommentLabel, 0, 1)
            self.windowLayout.addWidget(self.resultLabel, 1, 0)
            self.windowLayout.addWidget(self.extraCommentLine, 1, 1)
            self.windowLayout.addWidget(self.resultCombo, 1, 2)
        
        def combo_value(self):
            selected_option = self.resultCombo.currentText()
    #        if selected_option is "passed with additional comments" or "failed":
            if selected_option != "passed":
              self.extraCommentLine.setVisible(True)
              self.extraCommentLabel.setVisible(True)
    #        elif selected_option is "passed":
            elif selected_option == "passed":
              self.extraCommentLabel.setVisible(False)
              self.extraCommentLine.setVisible(False)                              # +
           
            
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        w = Window()
        w.resize(305, 70)
        w.show()
        sys.exit(app.exec_())
    

    【讨论】:

    • 是的,感谢您指出缺失的行。问题似乎还在于我在ifstatements 中使用了is 而不是==。我对 Python 有点陌生,我不知道这两者是不可互换的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多