tkinter 动态创建标签和条目 [英] tkinter create labels and entrys dynamically

查看:49
本文介绍了tkinter 动态创建标签和条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个简单的 GUI,我可以在其中输入一些值.之前和之后的标签以及启动脚本的按钮.

I want to create a simple GUI where I can enter some values. A label before and at the and an button to start the script.

我正在使用这样的东西:

I was using something like this:

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.get 一次.但我需要我所有的数据;-)

its ok but i want to do it dynamic. also when i would use w for all the entrys i only could cast w.get once. but i need all my data ;-)

我在想:

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

等等.我怎样才能让它工作?

and so on. how can i make it work?

推荐答案

你的程序建议 Entrybodyfat 和其他变量应该是 即时生成,但您不想这样做.

Your program suggests that Entrybodyfat and the other variables should be generated on the fly, but you don't want to do that.

通常的方法是将条目和标签存储在列表或地图中:

The normal approach is to store the entries and labels in a list or a map:

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

The value of the bodyfat entry is then entry["bodyfat"].get().

这篇关于tkinter 动态创建标签和条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆