Ungureanu Victor
Ungureanu Victor

Reputation: 52

tkinter concatenate entry name from string

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")

Upvotes: 0

Views: 205

Answers (2)

Merkle Daamgard
Merkle Daamgard

Reputation: 86

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'})

Upvotes: 1

Reblochon Masque
Reblochon Masque

Reputation: 36682

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()

Upvotes: 2

Related Questions