一、下拉列表框QComboBox
QComboBox是一个集按钮和下拉选项于一体的控件,也称为下拉列表框。
常用的方法
- addItem() 添加一个下拉选项
- addItems() 从列表中添加下拉选项
- Clear() 删除下拉选项中集中的所有选项
- count() 返回下拉选项集合中的数目
- currentText() 返回选中选项的文本
- itemText(i) 获取索引为i的item的选项文本
- currentIndex() 返回选中项的索引
- setItemText(int index, text) 改变序号为index项的文本
常用的信号
- 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 | 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_()) |
二、下拉多选
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 45 46 | from PyQt5.QtWidgets import QComboBox, QListWidgetItem, QListWidget, QCheckBox, QApplication, QVBoxLayout, QWidget,QPushButton,QHBoxLayout,QLineEdit import sys class ComboCheckBox(QWidget): def __init__( self , parent = None ): super (ComboCheckBox, self ).__init__(parent) self .items = [ '语文' , '数学' , '英语' , '体育' ] self .box_list = [] self .comb = QComboBox( self ) #列表框QComboBox self .listwidget = QListWidget( self ) #(列表控件) for i in range ( len ( self .items)): self .box_list.append(QCheckBox( self )) self .box_list[i].setText( self .items[i]) item = QListWidgetItem( self .listwidget) self .listwidget.setItemWidget(item, self .box_list[i]) # QComboBox添加模型和视图,QListWidget设置为QComboBox的View,QListWidget的Model设置为QComboBox的Model self .comb.setModel( self .listwidget.model()) self .comb.setView( self .listwidget) self .btn = QPushButton( '选择' , self ) self .btn.clicked.connect( self .get_selected) self .line = QLineEdit() #布局 layout = QVBoxLayout() layout.addWidget( self .btn) layout.addWidget( self .comb) layout.addWidget( self .line) self .setLayout(layout) def get_selected( self ): ret = [] for i in range ( len ( self .items)): if self .box_list[i].isChecked(): ret.append( self .box_list[i].text()) self .line.setText( str (ret)) if __name__ = = "__main__" : app = QApplication(sys.argv) ui = ComboCheckBox() ui.show() sys.exit(app.exec_()) |
到此这篇关于Python pyqt5下拉多选框的实现示例的文章就介绍到这了,更多相关Python pyqt5下拉多选框内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!