Python tkinter动态创建标签和入口
我想创建一个简单的GUI,在这里我可以输入一些值。前面和后面的标签以及启动脚本的按钮 我用的是这样的东西:Python tkinter动态创建标签和入口,python,tkinter,Python,Tkinter,我想创建一个简单的GUI,在这里我可以输入一些值。前面和后面的标签以及启动脚本的按钮 我用的是这样的东西: w = Label(master, text="weight:") w.grid(sticky=E) w = Label(root, text="bodyfathydrationmuscle:bones") w.grid(sticky=E) w = Label(root, text="hydration:") w.grid(sticky=E) 这没关系,但我想做它的动态。而且当我在所有入
w = Label(master, text="weight:")
w.grid(sticky=E)
w = Label(root, text="bodyfathydrationmuscle:bones")
w.grid(sticky=E)
w = Label(root, text="hydration:")
w.grid(sticky=E)
这没关系,但我想做它的动态。而且当我在所有入口使用w时,我只能施放一次w。但我需要我所有的数据;-)
我在想:
def create_widgets(self):
L=["weight","bodyfat","hydration","muscle","bones"]
LV=[]
for index in range(len(L)):
print(index)
print(L[index])
("Entry"+L[index])= Entry(root)
("Entry"+L[index]).grid(sticky=E)
("Label"+L[index])=Label(root, text=L[index])
("Label"+L[index]).grid(row=index, column=1)
稍后致电:
var_weight=Entryweight.get()
var_bodyfat=Entrybodyfat.get()
等等。如何使其工作?您的程序建议使用
Entrybodyfat
和其他变量,但您不想这样做
通常的方法是将条目和标签存储在列表或地图中:
from Tkinter import *
root = Tk()
names = ["weight", "bodyfat", "hydration", "muscle", "bones"]
entry = {}
label = {}
i = 0
for name in names:
e = Entry(root)
e.grid(sticky=E)
entry[name] = e
lb = Label(root, text=name)
lb.grid(row=i, column=1)
label[name] = lb
i += 1
def print_all_entries():
for name in names:
print entry[name].get()
b = Button(root, text="Print all", command=print_all_entries)
b.grid(sticky=S)
mainloop()
bodyfat条目的值就是entry[“bodyfat”]。get()