How do you replace a label in Tkinter python?

心不动则不痛 提交于 2019-12-02 11:55:37

There are a couple of simple ways to accomplish this. In both cases, it involves creating a label once, and then dynamically changing the text that is displayed.

Method 1: use a textvariable

If you associate a StringVar with a label, whenever you change the value of the StringVar, the label will be automatically updated:

labelVar = StringVar()
label = Label(..., textvariable=labelVar)
...
# label is automatically updated by this statement:
labelVar.set(newValue)

Method 2: update the text with the configure method:

label = Label(...)
...
# update the label with the configure method:
label.configure(text=newValue)

In both cases you need to make sure the object that you're changing (either the widget or the StringVar) is either a global variable or an instance variable so that you can access it later in your code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!