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_())
import sys
from pprint import pprint
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QComboBox-模型操作、视图操作")
self.resize(500, 500)
self.move(400, 250)
self.setup_ui()
def setup_ui(self):
cbb = QComboBox(self)
cbb.move(100, 100)
cbb.resize(200, 20)
model = QStandardItemModel()
item_1 = QStandardItem("item_1")
item_2 = QStandardItem("item_2")
item_22 = QStandardItem("item_22")
item_2.appendRow(item_22)
model.appendRow(item_1)
model.appendRow(item_2)
cbb.setModel(model)
cbb.setView(QTreeView(cbb))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
原文地址:https://blog.csdn.net/geji001/article/details/134700760
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_32644.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。