从零开始学Pyqt5之【控件介绍】(7):QComboBox下拉框列表控件
原创
©著作权归作者所有:来自51CTO博客作者阿旭算法与机器学习的原创作品,请联系作者获取转载授权,否则将追究法律责任
从零开始学Pyqt5之【控件介绍】(7):QComboBox下拉框列表控件
- QComboBox控件的作用
- QComboBox控件常用方法
- QComboBox控件常用信号
- 代码示例
QComboBox控件的作用
QcomcoBox控件通常用作下拉框选项,用于选择某个选项类别。
QComboBox控件常用方法

QComboBox控件常用信号

代码示例
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class ComboxDemo(QWidget):
def __init__(self, parent=None):
super(ComboxDemo, self).__init__(parent)
self.setWindowTitle("combox 示例")
self.resize(300, 90)
layout = QVBoxLayout()
self.lbl = QLabel("" )
self.cb = QComboBox()
self.cb.addItem("C")
self.cb.addItem("C++")
self.cb.addItems(["Java", "C#", "Python"])
self.cb.currentIndexChanged.connect(self.selectionchange)
layout.addWidget(self.cb)
layout.addWidget(self.lbl )
self.setLayout(layout)
def selectionchange(self,i):
self.lbl.setText( self.cb.currentText() )
self.lbl.adjustSize()
print( "Items in the list are :" )
for count in range(self.cb.count()):
# 打印出所有的下拉框索引和名称
print( 'item'+str(count) + '='+ self.cb.itemText(count) )
print( "Current index",i,"selection changed ",self.cb.currentText() )
if __name__ == '__main__':
app = QApplication(sys.argv)
comboxDemo = ComboxDemo()
comboxDemo.show()
sys.exit(app.exec_())
代码运行结果:

选择一个选项后:

打印出的信息如下:
