Tkinter:为动态生成的标签配置方法 [英] Tkinter: Configure method for labels dynamically generated

查看:26
本文介绍了Tkinter:为动态生成的标签配置方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用配置方法更改我的应用程序的标签.标签是在 for 循环中动态生成的.以下是部分代码:

I am trying to change the labels of my application with the configure method. The Labels are dynamically made in a for loop. Here is part of the code:

# create a list of reference for labels equal to zero
self.lbl_areas = []
for i in range(0, len(self.samples)): # number of labels
    lbl=tk.IntVar()
    lbl.set(0)
    self.lbl_areas.append(tk.Label(self.win,textvariable=lbl))

# Place labels on the application using grid             
for i,v in enumerate(self.lbl_areas):
    v.grid(row=2+i,column=1,sticky=tk.W)

# Try to change the value
for i in range(0, len(self.samples)):
    self.lbl_areas[i].configure(textvariable=lbl_val[i]) # other values

显示了默认的零值,但配置方法似乎不起作用.我做错了什么?

The default zero values are displayed but the configure method seems not to work. What do I do wrong?

推荐答案

有两种方法可以在标签创建后更新标签.第一种是使用文本变量,您可以在其中更新变量,标签会自动获取更改.第二种情况是不使用文本变量,而只是更改标签的文本.您正在尝试将两者混合.

There are two ways to update a label after it's been created. The first is to use a textvariable, where you update the variable and the label automatically picks up the change. The second is where you don't use a textvariable, and instead just change the label's text. You're trying to mix the two.

在我看来,最好的方法是不要使用文本变量.这是一个您需要跟踪的额外对象,它不会提供额外的好处(无论如何,在这种情况下).

In my opinion, the best way is to not use a textvariable. It's an extra object you need to keep track of which provides no extra benefit (in this case, anyway).

在你的情况下,我会写这样的代码:

In your case I would write the code like this:

for i in range(0, len(self.samples)): # number of labels  
    self.lbl_areas.append(tk.Label(self.win,text="0"))
...
for i in range(0, len(self.samples)):
    self.lbl_areas[i].configure(text=lbl_val[i]) 

如果要使用textvariable属性,则需要保存对变量的引用,以便以后设置:

If you want to use the textvariable attribute, then you need to save a reference to the variable so that you can set it later:

for i in range(0, len(self.samples)): # number of labels
    lbl=tk.IntVar()
    lbl.set(0)
    self.lbl_areas.append(tk.Label(self.win,textvariable=lbl))
    self.lbl_vars.append(lbl)
...

for i in range(0, len(self.samples)):
    self.lbl_vars[i].set(lbl_val[i]) 

请注意,在这两种情况下,您都必须调用函数(configureset)来更改值.您可以在小部件 (widget.configure(...)) 或变量 (var.set(...)) 上调用它.除非您利用 tkinter 变量的特殊属性——例如在两个或多个小部件之间共享变量,或使用变量跟踪——否则如果没有 textvariable,您的代码将不那么复杂.

Notice that in both cases, you must call a function (configure or set) to change the value. You either call it on the widget (widget.configure(...)) or on the variable (var.set(...)). Unless you're taking advantage of the special properties of a tkinter variable -- such as sharing the variable between two or more widgets, or using variable traces -- your code will be less complicated without the textvariable.

这篇关于Tkinter:为动态生成的标签配置方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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