赞
踩
目录
下面我们来实操一下,实现选中不同下拉框,打印不同的标签值
在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信号
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。