Python Qt GUI设计:QComboBox下拉列表框类(基础篇—14)

QComboBox是一个集按钮和下拉选项于一体的控件,也被称为下拉列表框。

QComboBox类中的常用方法如下表所示:

QComboBox类中的常用信号如下表所示: 

来看看QComboBox按钮类的示例,效果如下所示:

在这个例子中显示了一个下拉列表框和一个标签,其中下拉列表框中有5个选项,既可以使用QComboBox的addltem()方法添加单个选项,也可以使用addltems()方法添加多个选项,标签显示的是从下拉列表框中选择的选项。

当下拉列表框中的选项发生改变时将发射currentIndexChanged信号,连接到自定义的槽函数selectionchange()。

在方法中,当选中下拉列表框中的一个选项时,将把该选项的文本设置为标签的文本并调整标签的大小。

实现代码如下所示:

```python 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_()) ```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_44079197

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值