PyQt5 组件之QComboBox

本文介绍了PyQt5中的QComboBox组件,它是一个结合了按钮和下拉列表功能的控件。QComboBox提供了添加、删除和获取选项的方法,如addItem()、addItems()和clear()等。同时,详细阐述了其常用信号,如currentIndexChanged,当用户选择不同选项时触发。文中给出了一个实际示例,展示了如何在QComboBox中添加条目,监听选中项变化,并更新关联标签的文本。通过PyUIC转换的代码展示了如何在Python中实现这些功能。

QComboBox简介

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

QComboBox 常用方法

方法描述
addItem()添加一个下拉选项
addItems()从列表中添加下拉选项
Clear()删除下拉选项集合中的所有选项
count()返回下拉选项集合中的数目
currentText()返回选中选项的文本
itemText(i)获取索引为i的item的选项文本
currentIndex()返回选中项的索引
setItemText(int index,text)改变序列号为index的文本

QComboBox类中的常用信号

        

信号描述
Activated当用户选中一个下拉选项时发射该信号
currentIndexChanged当下拉选项的索引发生改变时发射该信号
highlighted当选中一个已经选中的下拉选项时,发射该信号

QComboBox效果截图:

  功能描述:点击QComboBox项,修改label 文本值,控制台输出点击相关数据信息。 

PyQt 模型设计:

PyQt 设计器截图: 

  *.ui 转换为*.py 代码 

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled4.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.
import sys

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.widget = QtWidgets.QWidget(self.centralwidget)
        self.widget.setGeometry(QtCore.QRect(150, 60, 131, 22))
        self.widget.setObjectName("widget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label = QtWidgets.QLabel(self.widget)
        self.label.setObjectName("label")
        self.horizontalLayout.addWidget(self.label)
        self.comboBox = QtWidgets.QComboBox(self.widget)
        self.comboBox.setObjectName("comboBox")
        self.horizontalLayout.addWidget(self.comboBox)

        # 单个添加条目
        self.comboBox.addItem('语文')
        self.comboBox.addItem('数学')
        self.comboBox.addItem('英语')
        # 多个添加条目
        self.comboBox.addItems(['物理', '化学', '生物'])
        # 添加监听事件
        # 当下拉索引发生改变时发射信号触发绑定的事件
        self.comboBox.currentIndexChanged.connect(self.selectionchange)

        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "默认Label"))

    def selectionchange(self, i):
        # 标签用来显示选中的文本
        # currentText():返回选中选项的文本
        self.label.setText(self.comboBox.currentText())
        print('Items in the list are:')
        # 输出选项集合中每个选项的索引与对应的内容
        # count():返回选项集合中的数目
        for count in range(self.comboBox.count()):
            print('Item' + str(count) + '=' + self.comboBox.itemText(count))
            print('current index', i, 'selection changed', self.comboBox.currentText())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

需要向PyUIC 生成的代码,添加如下代码片段:

重点实现QComboBox 数据源绑定和事件绑定

        # 单个添加条目
        self.comboBox.addItem('语文')
        self.comboBox.addItem('数学')
        self.comboBox.addItem('英语')
        # 多个添加条目
        self.comboBox.addItems(['物理', '化学', '生物'])
        # 添加监听事件
        # 当下拉索引发生改变时发射信号触发绑定的事件
        self.comboBox.currentIndexChanged.connect(self.selectionchange)
 def selectionchange(self, i):
        # 标签用来显示选中的文本
        # currentText():返回选中选项的文本
        self.label.setText(self.comboBox.currentText())
        print('Items in the list are:')
        # 输出选项集合中每个选项的索引与对应的内容
        # count():返回选项集合中的数目
        for count in range(self.comboBox.count()):
            print('Item' + str(count) + '=' + self.comboBox.itemText(count))
            print('current index', i, 'selection changed', self.comboBox.currentText())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值