0

I'm building a GUI using Tkinter and I've to create class StringVar atributes depending on a variable called initial_numbers. If initial_numbers is 5, I've to create 5 StringVars() with the name self.string_var plus a number so I can identify them.

If:

self.initial_numbers = 5

I've to create

self.string_var0 = StringVar()
self.string_var1 = StringVar()
self.string_var2 = StringVar()
self.string_var3 = StringVar()
self.string_var4 = StringVar()

After creating them, I want to add them to a list called stringList

What I've tryed this, but of course it's not working.

self.initial_numbers = 5
stringList = []
for i in xrange(int(self.initial_numbers)):
    self.string_var+str(i) = StringVar()
    self.string_var+str(i).set("test")
    stringList.append(self.date_string+str(i))

Thanks in advance.

1 Answer 1

2

You already put them in a list; why create five individual variables ? Put them in the list directly, and use the index to access them.

self.string_var = []                        # create empty list
for i in xrange(self.initial_numbers):
    self.string_var.append(StringVar())     # append to list
    self.string_var[i].set("test")          # access var at index i
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.