PyQt5下拉列表实现及信号与槽的连接

目录

1、常用方法

2、常用信号

3、实操


1、常用方法

  • QComboBox() 创建一个下拉框对象
  • addItems 可以使用列表进行多个下拉框内容添加, 单个添加用addItem
  • currentIndexChanged 是用来获取当前选择下拉框的索引, 这也是这个"信号"
  • 槽函数需要 有个索引传参, 这样就便于信号和槽的关联
  • currentText() 返回选中项的文本
  • adjustSize() 设置可以让展示适应文本调整尺寸,让展示更美观一些
  • Clear() 删除下拉选项集合中的所有选项
  • count() 返回下来选项集合中的数目
  • itemText(i) 获取索引为i的选项文本
  • currentIndex() 获取选中项的索引
  • setItemText(int index,text) 更改索引为index项的文本为text

2、常用信号

  • Activated:当用户选中一个下拉选项时触发该信号
  • currentIndexChanged:当下拉选项的索引发生改变时触发该信号
  • highlighted:当选中一个已经选中的下来选项时,触发该信号

3、实操

下面我们来实操一下,实现选中不同下拉框,打印不同的标签值

在Qt Designer中进行设计界面

设计的界面代码如下:

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

# Form implementation generated from reading ui file 'untitled.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.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(630, 369)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.comboBox = QtWidgets.QComboBox(self.centralwidget)
        self.comboBox.setGeometry(QtCore.QRect(430, 40, 181, 51))
        font = QtGui.QFont()
        font.setPointSize(36)
        self.comboBox.setFont(font)
        self.comboBox.setObjectName("comboBox")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(50, 30, 331, 221))
        font = QtGui.QFont()
        font.setPointSize(50)
        self.label.setFont(font)
        self.label.setStyleSheet("background-color: rgb(255, 255, 255);")
        self.label.setText("")
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 630, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.comboBox.setItemText(0, _translate("MainWindow", "MD1"))
        self.comboBox.setItemText(1, _translate("MainWindow", "MD2"))
        self.comboBox.setItemText(2, _translate("MainWindow", "MD3"))

我们可以通过addItems或addItem增加选项(界面设计中是先增加,后设置名字)

self.combo = QComboBox()  
self.combo.addItems(['MD1', 'MD2', 'MD3'])  

我们设计的逻辑代码如下:

# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from untitled import Ui_MainWindow


class MyMainForm(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MyMainForm, self).__init__(parent)
        self.setupUi(self)

        self.comboBox.currentIndexChanged.connect(self.select_change)
        # self.comboBox.activated.connect(self.select_change) # 该代码也可以

    def select_change(self,index):
        if index==0:
            self.label.setText("MD1")
        elif index==1:
            self.label.setText("MD2")
        elif index == 2:
            self.label.setText("MD3")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    myWin = MyMainForm()
    myWin.show()
    sys.exit(app.exec_())

实现选中不同下拉框打印不同标签值,主要用的是currentIndexChanged信号、也可以使用activated信号

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
PyQt中,可以通过信号机制实现下拉列表的自动刷新。具体步骤如下: 1. 定义一个函数`refresh_part_box`,用于刷新下拉列表的选项。 2. 在初始化函数中,将下拉列表的选项与函数连接起来,以便在选项改变时自动刷新。 3. 在函数`refresh_part_box`中,根据当前选中的产品,获取对应的支持部件列表,并将其添加到下拉列表中。 下面是一个示例代码: ```python from PyQt5.QtWidgets import QApplication, QMainWindow, QComboBox class MainWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.productBox = QComboBox(self) self.productBox.addItem("Product 1") self.productBox.addItem("Product 2") self.productBox.currentIndexChanged.connect(self.refresh_part_box) self.partBox = QComboBox(self) self.partBox.setGeometry(50, 50, 150, 30) self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Auto Refresh ComboBox') self.show() def refresh_part_box(self): product = self.productBox.currentText() part_list = get_supported_parts(product) self.partBox.clear() for part in part_list: self.partBox.addItem(part) def get_supported_parts(product): # 根据产品获取支持的部件列表 # 这里只是一个示例,具体实现需要根据实际情况进行编写 if product == "Product 1": return ["Part 1", "Part 2", "Part 3"] elif product == "Product 2": return ["Part A", "Part B", "Part C"] else: return [] if __name__ == '__main__': app = QApplication([]) window = MainWindow() app.exec_() ``` 在上述示例中,`refresh_part_box`函数根据当前选中的产品,获取对应的支持部件列表,并将其添加到`partBox`下拉列表中。每当产品选项改变时,`currentIndexChanged`信号会触发`refresh_part_box`函数,从而实现下拉列表的自动刷新。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

清纯世纪

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

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

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

打赏作者

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

抵扣说明:

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

余额充值