fireball.1
fireball.1

Reputation: 1521

Call function via for loop with functons as a list of strings

I am trying to write a PyQt5 application. All the list items in list variables are LineEdit objects of a form. On calling a reset function, I wish to set them to specific values for which I am trying to write a for loop instead of hard coding.

However, I cant use the list items in variables as objects as they are object attributes in other file and doing

self.eval(variables[i]).setText(default_0_values[i])

does't work, since eval is not an attribute of self. However, if I run

self.nGASLineEdit.setText(2)

it works. But I cant seem to be able to automate it and I'm stuck. Need a work around this.

from PyQt5 import QtCore,QtGui,QtWidgets,uic

variables=[
'nGASLineEdit','nDELTALineEdit','iMIPLineEdit','nDVECLineEdit','nSEEDLineEdit','eSTARTLineEdit','eTHRMLineEdit','eCUTLineEdit','nGAS1LineEdit','nGAS2LineEdit','nGAS3LineEdit','nGAS4LineEdit','nGAS5LineEdit','nGAS6LineEdit','fRAC1LineEdit','fRAC2LineEdit','fRAC3LineEdit','fRAC4LineEdit','fRAC5LineEdit','fRAC6LineEdit','tEMPLineEdit','tORRLineEdit','eFIELDLineEdit','bMAGLineEdit','bTHETALineEdit','iWRITELineEdit','iPENLineEdit','dETEFFLineEdit','eXCWGHTLineEdit','kGASLineEdit','lGASLineEdit','lCMPLineEdit','lRAYLineEdit','lPAPLineEdit','lBRMLineEdit','iECASCLineEdit'
]

default_0_values=[
2,100,5,1,0,1.0,1.5,2.0,
2 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 
]

class Window(QtWidgets.QMainWindow):
    def __init__(self):
        super(Window,self).__init__()
        uic.loadUi('main.ui',self)
        self.pushButton_2.clicked.connect(lambda: self.reset())

    def select_func(self,func_name):
        return func_name

    def reset(self):
        global variables
        global default_0_values
        for i in range(len(variables)):
            self.eval(variables[i]).setText(default_0_values[i])




if __name__=='__main__':
    import sys
    app=QtWidgets.QApplication(sys.argv)
    window=Window()
    window.show()
    sys.exit(app.exec())

UI file in .ui format

UI file in .py format

Upvotes: 2

Views: 113

Answers (1)

eyllanesc
eyllanesc

Reputation: 244003

Using eval is a bad programming practice and in this case it is unnecessary, in addition to putting self.eval(...) these are indicating that eval is a function of the Window class and clearly it is not.. On the other hand if you only read variables it is not necessary to use global. Instead we can use getattr() as shown below:

from PyQt5 import QtCore,QtGui,QtWidgets,uic

variables=['nGASLineEdit','nDELTALineEdit','iMIPLineEdit','nDVECLineEdit','nSEEDLineEdit','eSTARTLineEdit','eTHRMLineEdit','eCUTLineEdit','nGAS1LineEdit','nGAS2LineEdit','nGAS3LineEdit','nGAS4LineEdit','nGAS5LineEdit','nGAS6LineEdit','fRAC1LineEdit','fRAC2LineEdit','fRAC3LineEdit','fRAC4LineEdit','fRAC5LineEdit','fRAC6LineEdit','tEMPLineEdit','tORRLineEdit','eFIELDLineEdit','bMAGLineEdit','bTHETALineEdit','iWRITELineEdit','iPENLineEdit','dETEFFLineEdit','eXCWGHTLineEdit','kGASLineEdit','lGASLineEdit','lCMPLineEdit','lRAYLineEdit','lPAPLineEdit','lBRMLineEdit','iECASCLineEdit']

default_0_values=[2,100,5,1,0,1.0,1.5,2.0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

class Window(QtWidgets.QMainWindow):
    def __init__(self):
        super(Window,self).__init__()
        uic.loadUi('main.ui',self)
        self.pushButton_2.clicked.connect(self.reset)

    def reset(self):
        for le, val in zip(variables, default_0_values):
            getattr(self, le).setText(str(val))

if __name__=='__main__':
    import sys
    app=QtWidgets.QApplication(sys.argv)
    window=Window()
    window.show()
    sys.exit(app.exec())

Upvotes: 2

Related Questions