Search code examples
pythonpyqtpyqt5qcombobox

How to apply texts from pyqt5 qtcomboBox for lists value


I got texts from qtcomboBox to get values from the list. when I use "if" and "elif" to get the text from the list was works. but I think that code somewhat long and redundant.

so I am trying to change the code to make it simple for future applications.

I would like to change the below code(2) somewhat simple using code (1). But it's not working.

(1)

def abcvalue(self):

    cityyear=self.combo_text()
    a1=(cityyear[0])
    b1=(cityyear[1])
    c1=(cityyear[2])
    a=float(a1)
    b=float(b1)
    c=float(c1)

    self.av.setText(str(a))
    self.bv.setText(str(b))
    self.cv.setText(str(c))

(2)

import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
Calui='../_uiFiles/IDF.ui'

Toronto_2yr=[11,22,33]
Toronto_100yr=[44,55,66]
Richmondhill_2yr=[77,88,99]
Richmondhill_100yr=[10,11,12]

class MyWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self, None)
        uic.loadUi(Calui, self)
        self.comboBox.activated[str].connect(self.combo_text)
        self.pushButton.clicked.connect(self.abcvalue)
    def combo_text(self):
        city = self.comboBox.currentText()
        year = self.comboBox_2.currentText()
        city_year=(city+year)
        return city_year

    def abcvalue(self):
        cityyear=self.combo_text()

        if cityyear=='Toronto2yr':
            a1=(Toronto_2yr[0])
            b1=(Toronto_2yr[1])
            c1=(Toronto_2yr[2])
            a=float(a1)
            b=float(b1)
            c=float(c1)

            self.lineEdit.setText(str(a))
            self.lineEdit_2.setText(str(b))
            self.lineEdit_3.setText(str(c))

        elif cityyear=='Toronto100yr':
            a1=(Toronto_100yr[0])
            b1=(Toronto_100yr[1])
            c1=(Toronto_100yr[2])
            a=float(a1)
            b=float(b1)
            c=float(c1)

            self.lineEdit.setText(str(a))
            self.lineEdit_2.setText(str(b))
            self.lineEdit_3.setText(str(c))

        elif cityyear=='Richmondhill2yr':
            a1=(Richmondhill_2yr[0])
            b1=(Richmondhill_2yr[1])
            c1=(Richmondhill_2yr[2])
            a=float(a1)
            b=float(b1)
            c=float(c1)

            self.lineEdit.setText(str(a))
            self.lineEdit_2.setText(str(b))
            self.lineEdit_3.setText(str(c))

        elif cityyear=='Richmondhill100yr':
            a1 = (Richmondhill_100yr[0])
            b1 = (Richmondhill_100yr[1])
            c1 = (Richmondhill_100yr[2])
            a = float(a1)
            b = float(b1)
            c = float(c1)

            self.lineEdit.setText(str(a))
            self.lineEdit_2.setText(str(b))
            self.lineEdit_3.setText(str(c))

Solution

  • use this dictionary

    d = {
        "Toronto_2yr":[11,22,33],
        "Toronto_100yr":[44,55,66],
        "Richmondhill_2yr":[77,88,99],
        "Richmondhill_100yr":[10,11,12]
    }
    

    and use this function I hope it works :)

    def abcvalue(self):
            cityyear=self.combo_text()
    
            a1=(d[cityyear][0])
            b1=(d[cityyear][1])
            c1=(d[cityyear][2])
            a=float(a1)
            b=float(b1)
            c=float(c1)
    
            self.lineEdit.setText(str(a))
            self.lineEdit_2.setText(str(b))
            self.lineEdit_3.setText(str(c))