在 Python TkInter 中更新标签文本 [英] Update Label Text in Python TkInter

查看:146
本文介绍了在 Python TkInter 中更新标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何可能的方法来创建使用字符串和变量作为文本的 TkInter 标签?

Is there any possible way to create a TkInter label that uses a string and a variable as the text?

例如:

name = "bob"
Label(root, text="hello, my name is "+name)

但不是仅在标签创建时将标签文本设为上述内容,而是在名称"更改时更新文本,而无需引用标签实例本身.

But instead of that only making the text of the label to the above on label creation, to have it update the text when "name" changes without having to reference the label instance itself.

有人知道这是否可能吗?

Does anyone know if this is possible?

推荐答案

你不能完全按照你的要求去做——你不能同时将静态字符串和变量与标签相关联.您可以采取一些措施来获得所需的效果,但您所做的只是增加了复杂性而没有真正的收益.例如,您可以将 StringVar 的实例分配给 Label 小部件的 textvariable 属性.当您这样做时,对变量的任何更新都会更新标签.但是,您最终必须进行函数调用来更新变量,因此与进行函数调用以直接更新标签相比,您并没有真正获得任何好处.

You cannot do precisely what you ask -- you can't associate both a static string and a variable to a label at the same time. There are things you can do to get the desired effect, but all you're doing is adding complexity with no real gain. For example, you can assign an instance of StringVar to the textvariable attribute of the Label widget. When you do that, any update to the variable will update the label. However, you end up having to make a function call to update the variable, so you don't really gain anything over making a function call to update the label directly.

另一种选择是使用两个标签——一个用于静态文本,一个用于变量.将它们并排放置,没有边框,这样用户就不会注意到.然后,当您更新变量时,您将获得所需的效果.但是,您仍然需要调用函数来设置变量,因此您并没有真正获得太多收益.

Another option is to use two labels -- one for the static text and one for the variable. Put them side-by side with no border so the user won't notice. Then, when you update the variable you'll get the desired effect. However, you're still having to make a function call to set the variable so you don't really gain much.

另一种选择是使用 StringVar 的两个实例——一个用于标签,另一个用于名称.您可以在 name 变量上放置一个跟踪,以便当它发生变化时,您会使用静态字符串和 name 变量的值自动更新另一个变量,这将导致标签自动更新.然而,再次,您必须进行函数调用才能使一切都运转起来

Yet another option is to use two instances of StringVar -- one for the label, and another for the name. You can put a trace on the name variable so that when it changes, you automatically update the other variable with the static string and the value of the name variable, and that will cause the label to be automatically updated. Again, however, you're having to make a function call to put everything in motion

因此,如您所见,有多种选择,但它们都会增加您的代码的复杂性,与直接更新标签相比并没有真正的好处.只有当值需要一次出现在多个小部件中时,这些其他方法才能为您带来优势.在这种情况下,您可以将变量与两个或多个小部件相关联,单个函数调用将更新所有关联的小部件.

So, as you can see, there are options, but they all add complexity to your code with no real gain over simply updating the label directly. The only time these other methods gain you an advantage is when the value needs to appear in more than one widget at once. In that case you can associate the variable with two or more widgets, and a single function call will update all associated widgets.

这篇关于在 Python TkInter 中更新标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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