Raj
Raj

Reputation: 3

Dynamic GUI page Attribute Error

I have a GUI page populated with required number of lineedit boxes with unique names. I want to use that name later on to assign text to corresponding lineedit box. Here is the code:

from PyQt4 import QtCore, QtGui
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_LoadsForm3_1(object):
    def setupUi(self, LoadsForm3_1):
        LoadsForm3_1.setObjectName(_fromUtf8("LoadsForm3_1"))
        LoadsForm3_1.resize(500, 500)
        self.scrollArea = QtGui.QScrollArea(LoadsForm3_1)
        self.scrollArea.setGeometry(QtCore.QRect(20, 180, 450, 250))
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
        self.scrollAreaWidgetContents = QtGui.QWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, -293, 250, 500))
     self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents"))
        self.gridLayout = QtGui.QGridLayout(self.scrollAreaWidgetContents)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))

        for i in range (0,10):
            lE="lE0"+str(i)+"_P01"+"E0"+str(i)
            self.lE =QtGui.QLineEdit(self.scrollAreaWidgetContents)
            self.lE.setObjectName(_fromUtf8(lE))
            self.gridLayout.addWidget(self.lE, i, 1, 1, 1)
            self.scrollArea.setWidget(self.scrollAreaWidgetContent
        self.lE01_P01E01.setText("xyz")

if __name__ == "__main__":
    import sys app =QtGui.QApplication(sys.argv)
    LoadsForm3_1 = QtGui.QDialog()
    ui= Ui_LoadsForm3_1()
    ui.setupUi(LoadsForm3_1)
    LoadsForm3_1.show()
    sys.exit(app.exec_())

But on execution it syas : AttributeError: 'Ui_LoadsForm3_1' object has no attribute 'lE01_P01E01'. I am new to Pyqt4 and i am using python 2.7. Any help is appreciated. Thanks!!!

Upvotes: 0

Views: 39

Answers (1)

101
101

Reputation: 8999

Your code is a little mixed up. You set an attribute lE to be 'lE01_P01E01', and self.lE to be a QLineEdit object, but you never set self.lE01_P01E01 so it doesn't exist when you try to set it's text. You probably mean something like:

    for i in range(1,10):
        lE = QtGui.QLineEdit(self.scrollAreaWidgetContents)
        lE.setObjectName(_fromUtf8(lE))
        self.gridLayout.addWidget(lE, i, 1, 1, 1)

        # e.g. set self.lE01_P01E01 to lE
        setattr(self, 'lE0' + str(i) + '_P01E0' + str(i), lE)

        self.scrollArea.setWidget(self.scrollAreaWidgetContent

    self.lE01_P01E01.setText("xyz")

BUT, you should not be editing your auto-generated UI file as it will be overwritten. Instead, call it from another .py file, like this.

Upvotes: 1

Related Questions