Patrick ONeill Patrick ONeill - 3 years ago 119
Python Question

Using variable in instance name of text widget in tkinter

I am trying to use a variable in a textbox instance name in order to shuffle through them in a for loop. For example, I have 14 text widgets(infoBox1 to InfoBox14) that I am trying to fill from a list. So what I am looking to do is the following:

for item in finalList:

self.infoBox(x).insert(END, item)

x += 1

Then just populate the boxes as x increases. Can someone help with this?

Answer Source

You don't need names to do such thing. You can put your widgets in a list then access those using indexes.

#you can create like this. Used -1 as index to access last added text widget
text_list = []
for idx in range(14):

#then you can easily select whichever you want just like accessing any item from a list

#or directly
for idx, item in enumerate(finalList):
    text_list[idx].insert("end", item)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download