Pyqt5_QComboBox
QComboBox
是一个集按钮和下拉选项于一体的控件,也称做下拉列表框
方法
addItem()
添加一个下拉选项
addItems()
从列表中添加下拉选项
Clear()
删除下拉选项集合中的所有选项
count()
返回下拉选项集合中的数目
currentText()
返回选中选项的文本
itemText(i)
获取索引为i的item的选项文本
currentIndex()
返回选中项的索引
setItemText(int index,text)
改变序列号为index的文本
信号
Activated
当用户选中一个下拉选项时发射该信号
currentIndexChanged
当下拉选项的索引发生改变时发射该信号
highlighted
当选中一个已经选中的下拉选项时,发射该信号
baudrate_label = QLabel(' 波 特 率 ')
bytesize_label = QLabel(' 数 据 位' )
parity_label = QLabel(' 校 验 位 ')
stopbit_label = QLabel(' 停 止 位 ')
self.serialPort_combox = QComboBox()
port_serial = mylib.getPort()
i = 0
for i in range(len(port_serial)):
self.serialPort_combox.addItem(str(port_serial[i]))#一个个添加
self.serialPort_combox.setCurrentIndex(len(port_serial)-1)#设置默认值为最后一个
self.baudrate_combox = QComboBox()
self.baudrate_combox.addItems(myconst.baudrate_list)#直接添加一个list
self.baudrate_combox.setCurrentText(myconst.baudrate_list[4])#设置默认选择
#for item in myconst.baudrate_list:
#baudrate_combox.addItem(str(item))
self.bytesize_combox = QComboBox()
self.bytesize_combox.addItems(myconst.bytesizes)
self.bytesize_combox.setCurrentText(myconst.bytesizes[3])#设置默认选择
self.parity_combox = QComboBox()
self.parity_combox.addItems(myconst.parity_list)
self.stopbit_combox = QComboBox()
self.stopbit_combox.addItems(myconst.stop_bits)
self.stopbit_combox.setCurrentIndex(0)
#将以上控件添加到formlayout布局中
flayout_1.addRow(serialPort_label, self.serialPort_combox)
flayout_1.addRow(baudrate_label, self.baudrate_combox)
flayout_1.addRow(bytesize_label, self.bytesize_combox)
flayout_1.addRow(parity_label, self.parity_combox)
flayout_1.addRow(stopbit_label, self.stopbit_combox)
#将布局添加到paraConf_combox (QGroupBox) 中
self.portSetting_groupbox.setLayout(flayout_1)
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import * class ComboxDemo(QWidget):
def __init__(self,parent=None):
super(ComboxDemo, self).__init__(parent)
#设置标题
self.setWindowTitle('ComBox例子')
#设置初始界面大小
self.resize(300,90) #垂直布局
layout=QVBoxLayout()
#创建标签,默认空白
self.btn1=QLabel('') #实例化QComBox对象
self.cb=QComboBox()
#单个添加条目
self.cb.addItem('C')
self.cb.addItem('C++')
self.cb.addItem('Python')
#多个添加条目
self.cb.addItems(['Java','C#','PHP'])
#当下拉索引发生改变时发射信号触发绑定的事件
self.cb.currentIndexChanged.connect(self.selectionchange) #控件添加到布局中,设置布局
layout.addWidget(self.cb)
layout.addWidget(self.btn1)
self.setLayout(layout) def selectionchange(self,i):
#标签用来显示选中的文本
#currentText():返回选中选项的文本
self.btn1.setText(self.cb.currentText())
print('Items in the list are:')
#输出选项集合中每个选项的索引与对应的内容
#count():返回选项集合中的数目
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_())
Pyqt5_QComboBox的更多相关文章
随机推荐
- 剑指offer---05---用栈实现队列
题意 给了两个栈去实现队列 分析 两个栈如下情况 1 2 4 3 这个时候就不能够把4插入到第二个弹出栈了否则弹出顺序出错. 所以这个时候就应该等第二个栈空了的时候再 ...
- Codeforce-Ozon Tech Challenge 2020-A. Kuroni and the Gifts
the i-th necklace has a brightness ai, where all the ai are pairwise distinct (i.e. all ai are diffe ...
- CodeForces-259B]Little Elephant and Magic Square
Little Elephant loves magic squares very much. A magic square is a 3 × 3 table, each cell contains ...
- Pthon学习相关
1. Cython入门教程:https://www.jianshu.com/p/cfcc2c04a6f5
- P4370 [Code+#4]组合数问题2
题目要求当\(0\leq a\leq b\leq n\)时,\(k\)个\(\tbinom{b}{a}\)的和的最大值 观察杨辉三角形,可以发现,最大的\(\tbinom{b}{a}\),为\(\tb ...
- Jmeter 插件图表分析
1.jp@gc - Actiive Threads Over Time:不同时间的活动用户数量展示(图表) 当前的时间间隔是1毫秒,在 setting 中可以设置时间间隔以及其他的参数,右击可以导出 ...
- RF(页面断言)
一.RF中断言方式 title should be(断言title与预期指定的title内容相等) Open Browser https://www.baidu.com/ gc Title Shoul ...
- Spring依赖注入—@Resource注解使用
1.@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必须要求依赖对象必须存在,如果要允许null 值,可以设置它的required属性为false,如:@Autowire ...
- Android原生多语言切换方案,兼容Android10
前言 一个应用若需要国际化,至少需要支持中文和英语这两种语言,而同时随着谷歌的系统的更新,安卓系统可以设置当前语言的首选语言.因此,本文立足于此,多语言的切换方案为:App固定的文字内容,跟随系统,中 ...
- 使用Jexus 容器化您的 Blazor 应用程序
在本文中,我们将介绍如何将 Blazor 应用程序放入Jexus 容器以进行开发和部署.我们将使用 .NET Core CLI,因此无论平台如何,使用的命令都将是相同的. Blazor 托管模型 B ...