PyQt5 组件之QComboBox

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())

猜你喜欢

转载自blog.csdn.net/zhouzhiwengang/article/details/119735750