在 pyqt5 中更改另一个 ComboBox 项时更改 ComboBox 项
Change ComboBox item when another ComboBox item is changed in pyqt5
我的 GUI 中有两个组合框。我想根据第一个组合框的选定值更改第二个组合框中的值。
就像第一个组合框包含值 a、b、c。 When a is selected, the second combobox will have values 1,2,3. When b is selected, the second combobox will have values 4,5,6 and so on.
我正在尝试将 if else 语句与 currentText() 方法一起使用,但它没有改变。
这是我的代码:
python ui:
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(832, 625)
Dialog.setStyleSheet("background-color: rgb(85, 255, 255);")
self.comboBox = QtWidgets.QComboBox(Dialog)
self.comboBox.setGeometry(QtCore.QRect(140, 190, 481, 41))
self.comboBox.setStyleSheet("background-color: rgb(255, 255, 255);")
self.comboBox.setObjectName("comboBox")
self.comboBox_2 = QtWidgets.QComboBox(Dialog)
self.comboBox_2.setGeometry(QtCore.QRect(140, 290, 481, 41))
self.comboBox_2.setStyleSheet("background-color: rgb(255, 255, 255);")
self.comboBox_2.setObjectName("comboBox_2")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
主要
class cbox(QtWidgets.QDialog, Ui_Dialog):
def __init__(self):
super(cbox,self).__init__()
self.setupUi(self)
list_1 = ['a', 'b', 'c']
self.comboBox.addItems(list_1)
list_2 = ['1','2','3']
list_3 = ['4','5', '6']
list_4 = ['7','8','9']
if str(self.comboBox.currentText()) == 'a':
self.comboBox_2.addItems(list_2)
if str(self.comboBox.currentText()) == 'b':
self.comboBox_2.addItems(list_3)
if str(self.comboBox.currentText()) == 'c':
self.comboBox_2.addItems(list_4)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
mwin = cbox()
mwin.show()
sys.exit(app.exec_())
我怎样才能正确地做到这一点?
您必须使用 currentIndexChanged
信号,并连接到更新另一个组合的内容的函数。
class cbox(QtWidgets.QDialog, Ui_Dialog):
def __init__(自我):
超级(cbox,自我)。__init__()
self.setupUi(自己)
list_1 = ['a', 'b', 'c']
self.comboBox.addItems(list_1)
list_2 = ['1','2','3']
list_3 = ['4','5', '6']
list_4 = ['7','8','9']
自我.comboBox_2.addItems(list_2)
<b>self.sub_lists = list_2, list_3, list_4
self.comboBox.currentIndexChanged.connect(self.updateCombo)</b>
def updateCombo(自我,索引):
# 首先,清除组合的当前内容
自我.comboBox_2.clear()
# 然后,根据新索引添加新项目
self.comboBox_2.addItems(self.sub_lists[index])
我的 GUI 中有两个组合框。我想根据第一个组合框的选定值更改第二个组合框中的值。
就像第一个组合框包含值 a、b、c。 When a is selected, the second combobox will have values 1,2,3. When b is selected, the second combobox will have values 4,5,6 and so on.
我正在尝试将 if else 语句与 currentText() 方法一起使用,但它没有改变。
这是我的代码:
python ui:
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(832, 625)
Dialog.setStyleSheet("background-color: rgb(85, 255, 255);")
self.comboBox = QtWidgets.QComboBox(Dialog)
self.comboBox.setGeometry(QtCore.QRect(140, 190, 481, 41))
self.comboBox.setStyleSheet("background-color: rgb(255, 255, 255);")
self.comboBox.setObjectName("comboBox")
self.comboBox_2 = QtWidgets.QComboBox(Dialog)
self.comboBox_2.setGeometry(QtCore.QRect(140, 290, 481, 41))
self.comboBox_2.setStyleSheet("background-color: rgb(255, 255, 255);")
self.comboBox_2.setObjectName("comboBox_2")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
主要
class cbox(QtWidgets.QDialog, Ui_Dialog):
def __init__(self):
super(cbox,self).__init__()
self.setupUi(self)
list_1 = ['a', 'b', 'c']
self.comboBox.addItems(list_1)
list_2 = ['1','2','3']
list_3 = ['4','5', '6']
list_4 = ['7','8','9']
if str(self.comboBox.currentText()) == 'a':
self.comboBox_2.addItems(list_2)
if str(self.comboBox.currentText()) == 'b':
self.comboBox_2.addItems(list_3)
if str(self.comboBox.currentText()) == 'c':
self.comboBox_2.addItems(list_4)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
mwin = cbox()
mwin.show()
sys.exit(app.exec_())
我怎样才能正确地做到这一点?
您必须使用 currentIndexChanged
信号,并连接到更新另一个组合的内容的函数。
class cbox(QtWidgets.QDialog, Ui_Dialog):
def __init__(自我):
超级(cbox,自我)。__init__()
self.setupUi(自己)
list_1 = ['a', 'b', 'c']
self.comboBox.addItems(list_1)
list_2 = ['1','2','3']
list_3 = ['4','5', '6']
list_4 = ['7','8','9']
自我.comboBox_2.addItems(list_2)
<b>self.sub_lists = list_2, list_3, list_4
self.comboBox.currentIndexChanged.connect(self.updateCombo)</b>
def updateCombo(自我,索引):
# 首先,清除组合的当前内容
自我.comboBox_2.clear()
# 然后,根据新索引添加新项目
self.comboBox_2.addItems(self.sub_lists[index])