Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在PyQt5中动态更新QComboBox?_Python_Pyqt5_Qcombobox - Fatal编程技术网

Python 如何在PyQt5中动态更新QComboBox?

Python 如何在PyQt5中动态更新QComboBox?,python,pyqt5,qcombobox,Python,Pyqt5,Qcombobox,例如,第一个组合框带有选项1到10,另一个组合框也是1到10,当我在第一个组合框中选择2时,我需要第二个组合框显示选项3到10,我的意思是第二个组合框的值应该大于第一个。那么我应该如何在PyQt5中初始化组合框呢?您必须首先在\uuuuu init\uuu方法中初始化组合框 def __init__(self): #Write your __init__ body here self.comboBox1 = QComboBox() self.comboBox2 = QCo

例如,第一个组合框带有选项1到10,另一个组合框也是1到10,当我在第一个组合框中选择2时,我需要第二个组合框显示选项3到10,我的意思是第二个组合框的值应该大于第一个。那么我应该如何在PyQt5中初始化组合框呢?

您必须首先在
\uuuuu init\uuu
方法中初始化组合框

def __init__(self):
    #Write your __init__ body here
    self.comboBox1 = QComboBox()
    self.comboBox2 = QComboBox()
def updateComboBox2(self, newValue):
     ''' New Value is the changed value of comboBox1
         Do whatever you want to do with it here
         for eg. '''
     if newValue == 2:
         self.comboBox2.clear()        #This will remove all previous items
         for j in range(3,11):
              self.comboBox2.addItem(str(j))
然后您可以将第一个组合框的项目设置为1-10

for i in range(1,11):
    self.comboBox1.addItem(str(i))
您可以使用
currentTextChanged
信号更新comboBox2

self.comboBox1.currentTextChanged.connect(self.updateComboBox2)
currentTextChanged
信号将新选择的文本值传递给方法

所以
updateCombox2
方法

def __init__(self):
    #Write your __init__ body here
    self.comboBox1 = QComboBox()
    self.comboBox2 = QComboBox()
def updateComboBox2(self, newValue):
     ''' New Value is the changed value of comboBox1
         Do whatever you want to do with it here
         for eg. '''
     if newValue == 2:
         self.comboBox2.clear()        #This will remove all previous items
         for j in range(3,11):
              self.comboBox2.addItem(str(j))

为什么你认为asker没有使用
\uuuu init\uuuu
?我刚才澄清了它应该在init中初始化,否则它将无法工作。我并没有假设asker没有使用
\uuuuu init\uuuuu
当你完整地写下你的答案时,你必须发布,因为你可能会认为你在回应其他东西。你最初的答案与我为什么投反对票的问题没有直接关系,因为这是一个糟糕的答案,你现在(完整的)答案要好得多。是的,我用制表符正确缩进,但它错发了。很抱歉。另一方面,我建议在添加新元素之前调用clear(),因为例如,如果在组合框中首先有从1到5的数字,在第一个选择框中是从1到4,那么在第二个组合框中是5,如果在第二个选项中选择项目2和答案,您将在第二个组合框中获得项目2、3、4、5和5,您可以看到有重复的项目。