python GUI庫圖形界面開發(fā)之PyQt5下拉列表框控件QComboBox詳細(xì)使用方法與實(shí)例
QComboBox是一個(gè)集按鈕和下拉選項(xiàng)于一體的控件,也稱做下拉列表框
QComboBox類中的常用方法如表
方法 描述 addItem() 添加一個(gè)下拉選項(xiàng) addItems() 從列表中添加下拉選項(xiàng) Clear() 刪除下拉選項(xiàng)集合中的所有選項(xiàng) count() 返回下拉選項(xiàng)集合中的數(shù)目 currentText() 返回選中選項(xiàng)的文本 itemText(i) 獲取索引為i的item的選項(xiàng)文本 currentIndex() 返回選中項(xiàng)的索引 setItemText(int index,text) 改變序列號(hào)為index的文本 QComboBox類中的常用信號(hào) 信號(hào) 含義 Activated 當(dāng)用戶選中一個(gè)下拉選項(xiàng)時(shí)發(fā)射該信號(hào) currentIndexChanged 當(dāng)下拉選項(xiàng)的索引發(fā)生改變時(shí)發(fā)射該信號(hào) highlighted 當(dāng)選中一個(gè)已經(jīng)選中的下拉選項(xiàng)時(shí),發(fā)射該信號(hào)下拉列表框控件QComboBox按鈕的使用實(shí)例
import sysfrom PyQt5.QtCore import *from PyQt5.QtWidgets import *from PyQt5.QtCore import *class ComboxDemo(QWidget): def __init__(self,parent=None): super(ComboxDemo, self).__init__(parent) #設(shè)置標(biāo)題 self.setWindowTitle(’ComBox例子’) #設(shè)置初始界面大小 self.resize(300,90) #垂直布局 layout=QVBoxLayout() #創(chuàng)建標(biāo)簽,默認(rèn)空白 self.btn1=QLabel(’’) #實(shí)例化QComBox對象 self.cb=QComboBox() #單個(gè)添加條目 self.cb.addItem(’C’) self.cb.addItem(’C++’) self.cb.addItem(’Python’) #多個(gè)添加條目 self.cb.addItems([’Java’,’C#’,’PHP’]) #當(dāng)下拉索引發(fā)生改變時(shí)發(fā)射信號(hào)觸發(fā)綁定的事件 self.cb.currentIndexChanged.connect(self.selectionchange) #控件添加到布局中,設(shè)置布局 layout.addWidget(self.cb) layout.addWidget(self.btn1) self.setLayout(layout) def selectionchange(self,i): #標(biāo)簽用來顯示選中的文本 #currentText():返回選中選項(xiàng)的文本 self.btn1.setText(self.cb.currentText()) print(’Items in the list are:’) #輸出選項(xiàng)集合中每個(gè)選項(xiàng)的索引與對應(yīng)的內(nèi)容 #count():返回選項(xiàng)集合中的數(shù)目 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_())
效果圖如下
在這個(gè)例子中顯示了一個(gè)下拉列表框和一個(gè)標(biāo)簽,其中下拉列表框中有幾個(gè)選項(xiàng),既可以使用QCombobox的addItem()方法添加單個(gè)選項(xiàng),也可以使用addItems()方法添加多個(gè)選項(xiàng):標(biāo)簽顯示的是從下拉列表框中選擇的選項(xiàng)
#單個(gè)添加條目 self.cb.addItem(’C’) self.cb.addItem(’C++’) self.cb.addItem(’Python’) #多個(gè)添加條目 self.cb.addItems([’Java’,’C#’,’PHP’])
當(dāng)下拉列表框選中的選項(xiàng)發(fā)生改變時(shí)將發(fā)射currentIndexChanged信號(hào),鏈接到自定義的槽函數(shù)selectionChange()
self.cb.currentIndexChanged.connect(self.selectionchange)
在方法中,當(dāng)選中下拉列表框中的一個(gè)選項(xiàng)時(shí),將把該選項(xiàng)文本設(shè)置為標(biāo)簽的文本,并調(diào)整標(biāo)簽的大小
def selectionchange(self,i): #標(biāo)簽用來顯示選中的文本 #currentText():返回選中選項(xiàng)的文本 self.btn1.setText(self.cb.currentText())
本文詳細(xì)介紹了PyQt5下拉列表框控件QComboBox詳細(xì)使用方法與實(shí)例,更多關(guān)于PyQt5下拉列表框控件QComboBox的知識(shí)請查看下面的相關(guān)鏈接
相關(guān)文章:
1. Python sorted排序方法如何實(shí)現(xiàn)2. Python基于requests實(shí)現(xiàn)模擬上傳文件3. ASP.NET MVC實(shí)現(xiàn)橫向展示購物車4. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無效問題5. python利用opencv實(shí)現(xiàn)顏色檢測6. Python文本文件的合并操作方法代碼實(shí)例7. Python 中如何使用 virtualenv 管理虛擬環(huán)境8. 通過CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效9. asp讀取xml文件和記數(shù)10. Python獲取B站粉絲數(shù)的示例代碼
