本篇文章给大家分享的是有关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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。