提供一个下拉列表供选择,这样就可以用很少的屏幕空间来显示当前选择项
QComboBox的函数
- addItem():向集合中添加字符串
- addItems():以list的形式添加多个项目
- Clear():清除所有项目
- count():计算项目总数
- currentText():取回当前所选项目的文本
- itemText():显示属于特定索引的文本
- currentIndex():当前所选项的索引
- setItemText():改变特定索引的文本
QComboBox的信号
- activated():用户选择了某项
- currentIndexChanged():当前索引被用户或程序改变
- highlighted():某项被高亮
栗子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| import sys from PyQt4.QtCore import * from PyQt4.QtGui import *
class window(QWidget): def __init__(self, parent=None): super(window, self).__init__(parent) self.init()
def init(self): layout = QHBoxLayout() self.label1 = QLabel() self.combodemo = QComboBox()
self.label1.setText(u"你最喜欢谁?") self.combodemo.addItems([u"濑亚美莉",u"小泽玛利亚",u"石原里美",u"桃谷绘里香",u"冲田杏梨"]) self.combodemo.addItem(u"泽尻绘里香") self.combodemo.currentIndexChanged.connect(self.selectionchange)
layout.addWidget(self.label1) layout.addWidget(self.combodemo)
self.setLayout(layout) self.setWindowTitle(u"下拉框demo")
def selectionchange(self, i): print "Items in the list are :"
for count in range(self.combodemo.count()): print str(self.combodemo.itemText(count).toUtf8()) print "Current index", i, "selection changed ", str(self.combodemo.currentText().toUtf8())
def main(): app = QApplication(sys.argv) ex = window() ex.show() sys.exit(app.exec_())
if __name__ == '__main__': main()
|