PYQT:选择某些下拉值时,使Qlabel消失了
PyQt: make QLabel disappear when certain drop-down value is chosen
我有一个 QDialog
,它有几个 QLineEdits
和一个下拉列表 (QComboBox),其中有 3 个条目:通过、通过附加评论和失败。
每当用户选择“失败”或“通过附加评论”时,我都会加入额外的 QLineEdit
以供评论。如果选择“通过”,则不会添加任何字段。
我设法实现了显示额外评论字段的部分,每当两个条目之一被选中时,通过使该字段在开始时不可见,并且一旦条目被选择,它就会变得可见,但我无法做到只要选择“通过”,字段就会再次消失。
这里有一些与之相关的片段:
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
缺少某些东西但不确定是什么。感谢任何提示!
我觉得你不见了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_())
我有一个 QDialog
,它有几个 QLineEdits
和一个下拉列表 (QComboBox),其中有 3 个条目:通过、通过附加评论和失败。
每当用户选择“失败”或“通过附加评论”时,我都会加入额外的 QLineEdit
以供评论。如果选择“通过”,则不会添加任何字段。
我设法实现了显示额外评论字段的部分,每当两个条目之一被选中时,通过使该字段在开始时不可见,并且一旦条目被选择,它就会变得可见,但我无法做到只要选择“通过”,字段就会再次消失。
这里有一些与之相关的片段:
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
缺少某些东西但不确定是什么。感谢任何提示!
我觉得你不见了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_())