简体   繁体   中英

multiple entry labels in tkinter

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM