Python

Tkinter Change Label Text

Tkinter Label Text can easily be changed by using the “config” function and then changing the “text” attribute to the new desired text. Alternatively, if the label text has been made using the “StringVar()” then the user can utilize the “set()” function to change the label Text.

As Labels are the most crucial components of a Graphical User Interface, therefore, it is quite important to know how to change the label text whenever required. If you are new to creating interfaces with python and its Tkinter library, then this post will demonstrate the different methods of changing the label text inside the Tkinter window.

How to Change Tkinter Label Text Using the config() Function?

As already mentioned above, the user can easily change the label text with the help of the config(). However, to demonstrate this, take this code to create a basic Tkinter Window with a label:

from tkinter import *
# Tkinter Window
tkWindow = Tk();
tkWindow.geometry("200x200")

# Tkinter Label
text1 = Label(tkWindow, text="Hello World!")
text1.pack()

tkWindow.mainloop()

In this code snippet:

  • A simple Tkinter Window is created with a height of 200 and a width of 200 as well.
  • A Label “text1” has been created and attached to the main window.

Running the above code will produce the following Tkinter Window:

To change this Label upon button press, add the following lines of code:

def change_text():
  text1.config(text= "You changed text")

Button(tkWindow, text='Change Text!', width=15, height=2,
    command=change_text).pack(expand=True)

In this code snippet:

  • A simple button has been added to the window that calls the “change_text” function.
  • The “change_text()” accesses the label variable “text1”, calls the config function, and changes the value of the “text” attribute.

The complete code snippet for this demonstration is as:

from tkinter import *
# Tkinter Window
tkWindow = Tk();
tkWindow.geometry("200x200")
text1 = Label(tkWindow, text="Hello World!")
text1.pack()
# Function to Change the Text
def change_text():
  text1.config(text= "You changed text")
#Define a button to change text
Button(tkWindow, text='Change Text!', width=15, height=2,
    command=change_text).pack(expand=True)
tkWindow.mainloop()

Running this code will produce the following result:

The output verifies that the Label text changes as soon as the button is pressed.

How to Change Tkinter Label Text Using the set() Function?

To demonstrate the working of the set() function, first, create a Tkinter window with label text created through StringVar() using the following lines of code:

from tkinter import *
# Tkinter Window
tkWindow = Tk();
tkWindow.geometry("200x200")
# Create StringVar variable
textString = StringVar()
#Give Text Value to StringVar Variable
textString.set("Hello, this Label is created through StringVar()")
#Create Label using StringVar Variable
Label(tkWindow,textvariable=textString).pack(expand=True)

tkWindow.mainloop()

In this above code:

  • A variable “textString” is created through the “StringVar()” function
  • Give value to “textString” using the “set()” function.
  • Create a Label by specifying the Tkinter window and set the “textvariable” attribute equal to “textString”.

Running the above code will produce the following output on the Tkinter window:

To change the Label text using the set() function, add in the following lines of code:

def change_text():
  textString.set("Google")
#Define a button to change text
Button(tkWindow, text='Change Text!', width=15, height=2,
  command=change_text).pack(expand=True)

In this code:

  • A button is created that will call the “change_text()” function
  • The change_text() function takes the StringVar variable “textString” and uses the “set()” function to change its text.

The complete code snippet for this example is as follows:

from tkinter import *
# Tkinter Window
tkWindow = Tk();
tkWindow.geometry("200x200")
# Create StringVar variable
textString = StringVar()
#Give Text Value to StringVar Variable
textString.set("Hello, this Label is created through StringVar()")
#Create Label using StringVar Variable
Label(tkWindow,textvariable=textString).pack(expand=True)

#Define function to change text of StringVar variable
def change_text():
  textString.set("Google")
#Define a button to change text
Button(tkWindow, text='Change Text!', width=15, height=2,
    command=change_text).pack(expand=True)

tkWindow.mainloop()

Running this complete code will produce the following outcome on the Tkinter Window:

The output confirms that the Label text inside the Tkinter window was changed as soon as the button was pressed.

Conclusion

Changing the Label Text inside Tkinter GUI is rather an easy task that can be done through the use of the config() function and the set() function. Changing the Label text is quite a useful action that the developer has to constantly perform to notify/inform the user about various actions and states. This post has clearly shown the two different ways of changing the Label text inside Tkinter.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.