0

I have three entries like this:

e1 = Entry(master, state="readonly")
e2 = Entry(master, state="readonly")
e3 = Entry(master, state="readonly")

Is there any way to use a variable or string to define name of the Entry like:

x=int(1)
e+str(x) = Entry(master, state="readonly")
Nae
  • 14,209
  • 7
  • 52
  • 79
  • Possible duplicate of [How can you dynamically create variables via a while loop?](https://stackoverflow.com/questions/5036700/how-can-you-dynamically-create-variables-via-a-while-loop) – Nae Jan 10 '18 at 16:04

2 Answers2

2

The typical way to achieve this is by using a collection (here a list):

num_entries = 3
entries = []
for _ in range(num_entries):
    entries.append(Entry(master, state="readonly"))

The you can access each Entry object via its index, or iterate over all entries:

entries[0].get()
for entry in entries:
    entry.get()
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
1

Why not use arrays and dictionnaries ? Here's a sample that could help you :

vect = []
mas = #whatever object it is
st = 'readonly'
vect.append(({'master':mas},{'state':st}))
#Then you can read the elements of your array with
i = 0
vect[i] # where i is the index of the element you want (here there's only one 

Which will print

({'master':#whats master}, {'state':'readonly'})