How to change the Tkinter label text

In this tutorial, we will introduce how to change the Tkinter label text by clicking a button.

Use Stringvar to change the Tkinter label text

StringVar is one type of Tkinter constructor to create the Tkinter string variable.

After we associate the StringVar variable to the Tkinter widgets, Tkinter will update this particular widget when the variable is modified.

import tkinter as tk

class Test():
    def __init__(self):
        self.root = tk.Tk()
        self.text = tk.StringVar()
        self.text.set("Test")
        self.label = tk.Label(self.root, textvariable=self.text)

        self.button = tk.Button(self.root,
                                text="Click to change text below",
                                command=self.changeText)
        self.button.pack()
        self.label.pack()
        self.root.mainloop()

    def changeText(self):
        self.text.set("Text updated")        

app=Test()
self.text = tk.StringVar()
self.text.set("Test")

The Tkinter constructor couldn’t initiate the string variable with the string like self.text = tk.StringVar().

We should call set method to set the StringVar value, like self.text = tk.StringVar().

self.label = tk.Label(self.root, textvariable=self.text)

It associates the StringVar variable self.text to the label widget self.label by setting textvariable to be self.text. The Tk toolkit begins to track the changes of self.text and will update the text self.label if self.text is modified.

Label text property to change the label text

Another solution to change the Tkinter label text is to change the text property of the label.

import tkinter as tk

class Test():
    def __init__(self):
        self.root = tk.Tk()
        self.label = tk.Label(self.root, text="Text")

        self.button = tk.Button(self.root,
                                text="Click to change text below",
                                command=self.changeText)
        self.button.pack()
        self.label.pack()
        self.root.mainloop()

    def changeText(self):
        self.label['text'] = "Text updated"        

app=Test()

The text of the label could be initiated with text="Text" and could also be updated by assigning the new value to the text key of the label object.

We could also change the text property by tk.Label.configure() method as shown below. It works the same with the above codes.

import tkinter as tk

class Test():
    def __init__(self):
        self.root = tk.Tk()
        self.label = tk.Label(self.root, text="Text")

        self.button = tk.Button(self.root,
                                text="Click to change text below",
                                command=self.changeText)
        self.button.pack()
        self.label.pack()
        self.root.mainloop()

    def changeText(self):
        self.label.configure(text="Text Updated")        

app=Test()

Related Articles - Tkinter Label

  • How to change the Tkinter label font size
  • How to hide, recover and delete Tkinter widgets
  • comments powered by Disqus