【pyqt5学习】——下拉框comboBox

# 向下拉框中添加选型,具体为在下拉框第index+1个选型设置为内容name
self.comboBox.addItem(name,index+1)
# 将下拉框中所有的选项删除
self.comboBox.clear()
# 根据索引获取当前的下拉框内容
index = self.comboBox.currentIndex()
text = self.comboBox.itemText(index)

# 下拉框自带的信号——currentIndexChanged

self.comboBox.currentIndexChanged.connect(self.indexChange)

其中self.indexChange()是自定义的槽函数,即事件,当下拉框中的索引项发生改变时就会触发信号【currentIndexChanged】,进而会执行槽函数【self.indexChange】

实例:

如上所述有下拉框有多个姓名,默认显示为下拉框的第一项,即index=0,其中每个姓名都有一个数字对应着,现在需要做的是当选择不同的人时,自动将对应的数字在label中显示出来,这时候就使用到了currentIndexChanged信号,部分代码如下:


	def indexChange(self):
		try:
			if self.comboBox.currentIndex() == None or self.comboBox.currentIndex() == 0:
				self.label_3.setText(str(0))
			else:
				self.label_3.setText(str(self.name_money[self.comboBox.itemText(self.comboBox.currentIndex())]))
		except Exception as e:
			pass

	def run(self):
		self.pushButton.clicked.connect(self.searchPeople_)
		self.comboBox.currentIndexChanged.connect(self.indexChange)

 

其他常用属性:

 

 参考:

PyQt5基本控件详解之QComboBox(九)_jia666666的博客-CSDN博客icon-default.png?t=LA92https://blog.csdn.net/jia666666/article/details/81534260

猜你喜欢

转载自blog.csdn.net/qq_45769063/article/details/121267593
今日推荐