简体   繁体   English

tkinter中的多个条目标签

[英]multiple entry labels in tkinter

I am a beginning (emphasis) programmer, and I am creating a study GUI application. 我是一名入门(强调)程序员,并且正在创建一个学习GUI应用程序。 I want to create multiple Entry fields for terms, and definitions. 我想为术语和定义创建多个Entry字段。 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. 假设您有用户输入x ,一个整数。

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. 然后,您可能需要循环内的名称,例如entry1, entry2, entry3 This relates dynamically variable creation which can not be accessed in Python. 这涉及动态变量创建,而该变量在Python中无法访问。 There is a hack way using exec or __dict__ , but it is not recommended. 有一种使用exec__dict__的hack方法,但不建议这样做。 Just use the list or dict. 只需使用列表或字典即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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