Declaring n objects from strings

Joined
Aug 26, 2008
Messages
1
Reaction score
0
Hi, I have a little problem designing an automatized GUI in PyQT.

The problem is like this: I don't know how many inputs I will have, and I first want my program to count the amount of inputs, then according to this number declare the corresponding amount of objects.

Something like this:

#Don't comment the code itself I know it's wrong. It's rather a half-pseudo implementation to show what I mean
for i in range(1,x):
...self.obj = QtGui.QLineEdit(self.tab)
...self.obj = eval("box_"+ str(i))
...#Here i want to create actually n global line edits addressed as "box_1", "box_2", etc
...self.obj.setGeometry(QtCore.QRect(2,0,181,20))
...self.obj.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
...self.obj.setObjectName("box_" + str(i))

The problem is eval does here something else I want, I normally used the getattr() function to get the object from a string but this is rather used to fish out existing objects, but here i want to declare an object having only a string that will be his name.
Actually after the self.obj = eval("box_"+ str(i)) command I want to start using self.box_i because the self.obj will be in the next loop overwritten.
I wonder if it is actually possible to do this in python.

Other alternative I see, would be to create a big predefined list of objects(as the number of inputs shouldn't exceed 100) and according to the number just add then the properties and destroy the unused objects, but the quality of code and optimization would suffer a lot then.


I'm open for any suggestions
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top