Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

python - Trying to take a value from a dictionary of a ComboBox and instert the key in a textEdit

I have a dictionary and a ComboBox that shows dictionary's values, i need to print in a textEdit the key of the dictionary's value chosen by the user. Here's my code.

import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow
from PyQt5.uic import loadUi
from math import pi

class MainPage(QMainWindow):

    classi = {None : None, "C25/30" : float(14.17), 
        "C28/35" : float(15.87), "C32/40" : float(18.13), 
        "C35/45" : float(19.83), "C40/50" : float(22.6),
        "C45/55" : float(25.5), "C50/60" : float(28.3)}

    Acciaio = {None : None, "B450C" : float(391.3)}

    Ferri = {14 : float(1.54), 16 : float(2.01),
         18 : float(2.54), 20 : float(3.14), 22 : float(3.8)}

    N = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    def __init__(self):
        super(MainPage, self).__init__()
        loadUi('Concrete.ui', self)
        x = self.fillCombobox()
        y = self.fillCombobox_2()
        z = self.fillCombobox_3()
        v = self.fillCombobox_4()
        w = self.fillCombobox_5()
        a = self.fillCombobox_6()
        b = self.fillCombobox_7()
        c = self.fillCombobox_8()


    def fillCombobox(self):
        for i in self.classi:
            self.comboBox.addItem(i)


    def fillCombobox_2(self):
        for i in self.Acciaio:
            self.comboBox_2.addItem(i)

    def retrieveText(self):
        x = self.comboBox.currentData()
        self.textEdit.setText(x)


app = QApplication(sys.argv)
widget = MainPage()
widget.show()
sys.exit(app.exec_())

I know that the function retrieveText doesn't make what i want but it was a try and also this doesn't give any output.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to save the values associated with the keys through itemData and obtain it when an item is selected.

class MainPage(QMainWindow):

    classi = {
        None: None,
        "C25/30": 14.17,
        "C28/35": 15.87,
        "C32/40": 18.13,
        "C35/45": 19.83,
        "C40/50": 22.6,
        "C45/55": 25.5,
        "C50/60": 28.3,
    }

    def __init__(self):
        super(MainPage, self).__init__()
        loadUi("Concrete.ui", self)
        self.comboBox.currentIndexChanged[int].connect(self.retrieveText)
        self.fillCombobox()

    def fillCombobox(self):
        for key, value in self.classi.items():
            self.comboBox.addItem(key, value)

    @pyqtSlot(int)
    def retrieveText(self, index):
        x = self.comboBox.itemData(index)
        if x is not None:
            self.textEdit.setText(str(x))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...