您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關QComboBox怎么在pyqt5中使用,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
類歸屬
PyQt5->QtWidgets->QComboBox
繼承關系
PyQt5->QObject and QPaintDevice->QWidget->QFontComboBox->QComboBox
熟悉一下代碼,直接就可以用了。
【如下代碼,完全復制,直接運行,即可使用】
import sys from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import * ################################################ items_list=["C","C++","Java","Python","JavaScript","C#","Swift","go","Ruby","Lua","PHP"] datas_list=[1972,1983,1995,1991,1992,2000,2014,2009,1995,1993,1995] ################################################ class Widget(QWidget): def __init__(self, *args, **kwargs): super(Widget, self).__init__(*args, **kwargs) layout = QVBoxLayout(self) self.combobox1 = QComboBox(self, minimumWidth=200) self.combobox2 = QComboBox(self, minimumWidth=200) self.combobox3 = QComboBox(self, minimumWidth=200) self.combobox4 = QComboBox(self, minimumWidth=200) layout.addWidget(QLabel("增加單項,不帶數據", self)) layout.addWidget(self.combobox1) layout.addItem(QSpacerItem(20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)) layout.addWidget(QLabel("增加單項,附帶數據", self)) layout.addWidget(self.combobox2) layout.addItem(QSpacerItem(20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)) layout.addWidget(QLabel("增加多項,不帶數據", self)) layout.addWidget(self.combobox3) layout.addItem(QSpacerItem(20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)) layout.addWidget(QLabel("設置模型,不帶數據", self)) layout.addWidget(self.combobox4) #初始化combobox self.init_combobox1() self.init_combobox2() self.init_combobox3() self.init_combobox4() #增加選中事件 self.combobox1.activated.connect(self.on_combobox1_Activate) self.combobox2.activated.connect(self.on_combobox2_Activate) self.combobox3.activated.connect(self.on_combobox3_Activate) self.combobox4.activated.connect(self.on_combobox4_Activate) ####### addItem() 增加單項元素,不帶數據 ######### def init_combobox1(self): for i in range(len(items_list)): self.combobox1.addItem(items_list[i]) self.combobox1.setCurrentIndex(-1) def on_combobox1_Activate(self, index): print(self.combobox1.count()) print(self.combobox1.currentIndex()) print(self.combobox1.currentText()) print(self.combobox1.currentData()) print(self.combobox1.itemData(self.combobox1.currentIndex())) print(self.combobox1.itemText(self.combobox1.currentIndex())) print(self.combobox1.itemText(index)) ####### addItem() 增加單項元素,附帶數據 ######### def init_combobox2(self): for i in range(len(items_list)): self.combobox2.addItem(items_list[i],datas_list[i]) self.combobox2.setCurrentIndex(-1) def on_combobox2_Activate(self, index): print(self.combobox2.count()) print(self.combobox2.currentIndex()) print(self.combobox2.currentText()) print(self.combobox2.currentData()) print(self.combobox2.itemData(self.combobox2.currentIndex())) print(self.combobox2.itemText(self.combobox2.currentIndex())) print(self.combobox2.itemText(index)) ####### addItems() 增加多項元素,不帶數據 ######### def init_combobox3(self): self.combobox3.addItems(items_list) self.combobox3.setCurrentIndex(-1) def on_combobox3_Activate(self, index): print(self.combobox3.count()) print(self.combobox3.currentIndex()) print(self.combobox3.currentText()) print(self.combobox3.currentData()) print(self.combobox3.itemData(self.combobox3.currentIndex())) print(self.combobox3.itemText(self.combobox3.currentIndex())) print(self.combobox3.itemText(index)) ####### setModel() 設置數據模型,不帶數據 ######### def init_combobox4(self): self.tablemodel = QStringListModel(items_list) self.combobox4.setModel(self.tablemodel) self.combobox4.setCurrentIndex(-1) def on_combobox4_Activate(self, index): print(self.combobox4.count()) print(self.combobox4.currentIndex()) print(self.combobox4.currentText()) print(self.combobox4.currentData()) print(self.combobox4.itemData(self.combobox4.currentIndex())) print(self.combobox4.itemText(self.combobox4.currentIndex())) print(self.combobox4.itemText(index)) if __name__ == "__main__": app = QApplication(sys.argv) w = Widget() w.show() sys.exit(app.exec_())
以上就是QComboBox怎么在pyqt5中使用,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。