pyqt5自定义列表控件(PyQt5学习记录下拉列表框QComboBox)
pyqt5自定义列表控件(PyQt5学习记录下拉列表框QComboBox)currentIndexChanged5、Label控件根据内容调整尺寸self.cb.currentText()4、信号(事件)self.cb = QComboBox()2、添加项目self.cb.addItem(‘C ’) self.cb.addItems([‘C ’ ’java’ ’Ruby’])注意区别,列表添加的有个s,并且列表中的内容只能是字符串,不能是数字3、获取选中的列表项

下拉列表框也是很常见的控件,如下图所示:

导入库:
from PyQt5.QtWidgets import QWidget QComboBox QLabel QVBoxLayout
    
知识点:
1、创建QComBox
self.cb = QComboBox()
    
2、添加项目
self.cb.addItem(‘C  ’)
self.cb.addItems([‘C  ’ ’java’ ’Ruby’])
    
注意区别,列表添加的有个s,并且列表中的内容只能是字符串,不能是数字
3、获取选中的列表项
self.cb.currentText()
    
4、信号(事件)
currentIndexChanged
    
5、Label控件根据内容调整尺寸
self.label.adjustSize()
    
6、项目总数
self.cb.count()
    
7、根据序号得到项目
self.cb.itemText(i)
    
以下为UI_form.py中部分代码:
def setupUI(self):
 self.setWindowTitle("下拉列表框QComboBox")
 # 创建下拉列表
 self.cb = QComboBox()
 # 添加一个项目
 self.cb.addItem("0")
 # 添加一组项目
 self.cb.addItems(["1" "2" "3" "4" "5"])
 # 绑定事件
 self.cb.currentIndexChanged.connect(self.changeSelection)
 # 创建标签
 self.label = QLabel("请选择:")
 # 创建布局框
 vbox = QVBoxLayout()
 vbox.addWidget(self.label)
 vbox.addWidget(self.cb)
 self.setLayout(vbox)
def changeSelection(self index):
 self.label.adjustSize()
 self.label.setText("当前内容:{}".format(self.cb.currentText()))
 print("当前内容:{}".format(self.cb.itemText(index)))          




