Reputation: 424
I am a beginning (emphasis) programmer, and I am creating a study GUI application. I want to create multiple Entry fields for terms, and definitions. To create one Entry box, the code ( I believe) would be:
term = StringVar()
term1 = Entry(root, textvariable = term)
term1.grid(row=1, column=1)
My goal is to be able to prompt the user asking how many terms they want. My question is what loop would I have to run to automatically create a column of entry fields, specific to the number the user entered?
Upvotes: 2
Views: 5541
Reputation: 8610
Assume you have got the user input x
, an integer.
for i in range(x):
Entry(root, textvariable=StringVar()).grid(row=1, column=i+1)
But unfortunately you can not get the value of the entries then. So we can take two lists.
variables = []
entries = []
for i in range(x):
va = StringVar()
en = Entry(root, textvariable=va)
en.grid(row=1, column=i+1)
variables.append(va)
entries.append(en)
In this case, you can access the entry and variable then using the lists.
Then you may want names, for example, entry1, entry2, entry3
, within the loop. This relates dynamically variable creation which can not be accessed in Python. There is a hack way using exec
or __dict__
, but it is not recommended. Just use the list or dict.
Upvotes: 2