Skip to content Skip to sidebar Skip to footer

How Do You Replace A Label In Tkinter Python?

I am a newbie programmer and I am making a currency converter....It is still in progress, but could anyone help me to try to replace the label made in 'def convert()'...To be clear

Solution 1:

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 bythis 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.

Post a Comment for "How Do You Replace A Label In Tkinter Python?"