Найти - Пользователи
Полная версия: pyqt5 получить значение QComboBox
Начало » GUI » pyqt5 получить значение QComboBox
1
Pacifist
Как в label получить значение из comboBox (словаря)? При выборе в comboBox Яблоко - в label должно быть 5

 import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(509, 137)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.comboBox = QtWidgets.QComboBox(self.centralwidget)
        self.comboBox.setGeometry(QtCore.QRect(40, 50, 171, 22))
        self.comboBox.setObjectName("comboBox")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(320, 50, 121, 21))
        self.label.setStyleSheet("background-color: rgb(110, 166, 255);")
        self.label.setText("")
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        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"))
list_name = {'Яблоко': 5, 'Дыня': 6, 'Груша': 7, 'Вишня': 9}
class Proba(QtWidgets.QMainWindow):
    def __init__(self):
        super(Proba, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.comboBox.addItems(list_name)
        value = self.ui.comboBox.currentData()
        self.ui.label.setText(str(value))
app = QtWidgets.QApplication([])
application = Proba()
application.show()
sys.exit(app.exec())
rami
 class Proba(QtWidgets.QMainWindow):
    def __init__(self):
        super(Proba, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.comboBox.addItems(list_name)
        self.ui.comboBox.currentIndexChanged[str].connect(self.text_to_label)
 
    def text_to_label(self, item_selected):
        self.ui.label.setText(str(list_name[item_selected]))

Остальное как у вас.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB