当前位置:   article > 正文

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

pyqt下拉列表

目录

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中进行设计界面

设计的界面代码如下:

  1. # -*- coding: utf-8 -*-
  2. # Form implementation generated from reading ui file 'untitled.ui'
  3. #
  4. # Created by: PyQt5 UI code generator 5.15.4
  5. #
  6. # WARNING: Any manual changes made to this file will be lost when pyuic5 is
  7. # run again. Do not edit this file unless you know what you are doing.
  8. from PyQt5 import QtCore, QtGui, QtWidgets
  9. class Ui_MainWindow(object):
  10. def setupUi(self, MainWindow):
  11. MainWindow.setObjectName("MainWindow")
  12. MainWindow.resize(630, 369)
  13. self.centralwidget = QtWidgets.QWidget(MainWindow)
  14. self.centralwidget.setObjectName("centralwidget")
  15. self.comboBox = QtWidgets.QComboBox(self.centralwidget)
  16. self.comboBox.setGeometry(QtCore.QRect(430, 40, 181, 51))
  17. font = QtGui.QFont()
  18. font.setPointSize(36)
  19. self.comboBox.setFont(font)
  20. self.comboBox.setObjectName("comboBox")
  21. self.comboBox.addItem("")
  22. self.comboBox.addItem("")
  23. self.comboBox.addItem("")
  24. self.label = QtWidgets.QLabel(self.centralwidget)
  25. self.label.setGeometry(QtCore.QRect(50, 30, 331, 221))
  26. font = QtGui.QFont()
  27. font.setPointSize(50)
  28. self.label.setFont(font)
  29. self.label.setStyleSheet("background-color: rgb(255, 255, 255);")
  30. self.label.setText("")
  31. self.label.setObjectName("label")
  32. MainWindow.setCentralWidget(self.centralwidget)
  33. self.menubar = QtWidgets.QMenuBar(MainWindow)
  34. self.menubar.setGeometry(QtCore.QRect(0, 0, 630, 22))
  35. self.menubar.setObjectName("menubar")
  36. MainWindow.setMenuBar(self.menubar)
  37. self.statusbar = QtWidgets.QStatusBar(MainWindow)
  38. self.statusbar.setObjectName("statusbar")
  39. MainWindow.setStatusBar(self.statusbar)
  40. self.retranslateUi(MainWindow)
  41. QtCore.QMetaObject.connectSlotsByName(MainWindow)
  42. def retranslateUi(self, MainWindow):
  43. _translate = QtCore.QCoreApplication.translate
  44. MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
  45. self.comboBox.setItemText(0, _translate("MainWindow", "MD1"))
  46. self.comboBox.setItemText(1, _translate("MainWindow", "MD2"))
  47. self.comboBox.setItemText(2, _translate("MainWindow", "MD3"))

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

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

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

  1. # -*- coding: utf-8 -*-
  2. import sys
  3. from PyQt5.QtWidgets import QApplication, QMainWindow
  4. from untitled import Ui_MainWindow
  5. class MyMainForm(QMainWindow, Ui_MainWindow):
  6. def __init__(self, parent=None):
  7. super(MyMainForm, self).__init__(parent)
  8. self.setupUi(self)
  9. self.comboBox.currentIndexChanged.connect(self.select_change)
  10. # self.comboBox.activated.connect(self.select_change) # 该代码也可以
  11. def select_change(self,index):
  12. if index==0:
  13. self.label.setText("MD1")
  14. elif index==1:
  15. self.label.setText("MD2")
  16. elif index == 2:
  17. self.label.setText("MD3")
  18. if __name__ == '__main__':
  19. app = QApplication(sys.argv)
  20. myWin = MyMainForm()
  21. myWin.show()
  22. sys.exit(app.exec_())

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

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号