Text or a picture can be shown on the screen using the Tkinter label widgets. Only one typeface can be displayed on a label. Multiple lines of text are possible. A label can include any text, and a window can contain many labels (just like any widget can be displayed multiple times in a window). You can easily change/update the Python Tkinter label text with the label text property. Changing the label’s text property is another way to change the Tkinter label text. This lesson will look at modifying label text when a button is clicked in Tkinter Python.

Labels in Tkinter

Python provides several alternatives for creating a graphical user interface. Tkinter is the most widely used GUI technique out of all the options. Using widgets, creating a GUI with Tkinter is simple. Widgets, such as buttons and menus, are common graphical user interface (GUI) elements.

Tkinter Label is a widget that lets you make text or graphics-based display boxes. At any time, the developer has the power to change the text displayed by this widget. It can also be used to execute operations like underlining text and spanning text across numerous lines.

It’s vital to remember that a label can only display text in one typeface at a time. All you must do to use a label is tell it what to display (this can be text, an image, or even a bitmap). Let’s have a look at some sample programs to see how you can update the label text.

Example 1:

Tkinter is used in the following Python script to produce a window with the text “Hello World.” You may either type this script line by line in the Python interpreter or save it as a file. The Tkinter module, which contains the Tk toolkit, must be imported at all times. We first imported the Tkinter module by renaming it to tk in our example below. It is the preferred method that all programmers should follow.

Tkinter is the main window with a title bar and other options the window manager provides. Only one root widget can be generated, and it must be established before any additional widgets.

The Label widget is found on the next line of code after the root widget. The name of the parent window, in our instance “root_mthd,” is the first parameter of the Label function. Resultantly, the Label widget descends from the root widget.

The text to be shown is specified by the keyword argument “text.” Tk uses the pack function to resize the window to fit the specified text. We won’t see the window until we’ve entered the Tkinter event loop. Our script will stay in the event loop until we close the window.

import tkinter as tk


root_mthd = tk.Tk()


w1 = tk.Label(root_mthd, text=“Hello World!”)


w1.pack()


root_mthd.mainloop()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Change-Label-Text-1.png" data-lazy- height="136" src="data:image/svg xml,” width=”516″>

After you’ve saved and run the script, you’ll get the following results.

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Change-Label-Text-2.png" height="58" src="data:image/svg xml,” width=”123″>

Example 2:

The Label.config() method is used in this example. This method is used to do a label widget overwriting. We used # to import everything from Tkinter and then created the Tkinter window in the code. After declaring the variable, we defined the function and wrote the code to update the label. It’s time to make a button and a label widget. The widgets were then placed in the GUI window, and the GUI was started. The whole code can be found below.

from tkinter import *


window1 = Tk()


text1 = “Tkinter Change Label Text Example”

def counter():


    global text1


    label1.config(text = text1)


button1 = Button(window1,


                text = “Update Text”,


                command = counter)


label1 = Label(window1,


                text = “Tkinter Change Label Text”)


label1.pack()


button1.pack()


window1.mainloop()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Change-Label-Text-3.png" data-lazy- height="361" src="data:image/svg xml,” width=”556″>

You can see the label and the button in the following output screen.

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Change-Label-Text-4.png" height="84" src="data:image/svg xml,” width=”149″>

When we click on the button, the label is successfully updated, as you can see.

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Change-Label-Text-5.png" height="79" src="data:image/svg xml,” width=”196″>

Example 3:

You can select the typefaces used to display text in some Tk widgets, such as the label, text, and canvas widgets. This is usually accomplished using a “font” configuration option. It’s important to remember that typefaces are one of few areas that aren’t platform-agnostic. The attribute “fg” can be utilized to change the text color. On the other hand, the attribute “bg” can be utilized to modify the label’s background color.

import tkinter as tk


root1 = tk.Tk()


tk.Label(root1,


         text=“Red Text in displayed in Times Font”,


         fg = “red”,


         font = “Times”).pack()


tk.Label(root1,


         text=“Green Text in displayed in Helvetica Font”,


         fg = “dark green”,


         bg = “light green”,


         font = “Helvetica 12 bold italic”).pack()


root1.mainloop()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Change-Label-Text-6.png" data-lazy- height="227" src="data:image/svg xml,” width=”555″>

Here’s what the above code produced in terms of colorful text.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Change-Label-Text-7.png" data-lazy- height="85" src="data:image/svg xml,” width=”317″>

Example 4:

Here’s a simple example of how to change/update the Tkinter label text using stringvar. StringVar is a type of Tkinter co-constructor that creates a string variable in Tkinter. When we connect with Tkinter widgets, Tkinter will change this particular widget when the StringVar variable is updated.

The string variable could not be started with the string self.t_text = tk.StringVar in the Tkinter constructor. To set the StringVar value, we should use the set method, such as self.t_text.set (“Test”).

By setting textvariable to self.t_text, it connects the StringVar variable (self.t_text) with the label widget indicated as self.l_label. If self.t_text is changed, the Tk toolkit starts tracking the changes and updates the text self.l_label. A Tkinter dynamic label is created with the code above. When self.t_text is changed, it displays the Tkinter label text immediately.

import tkinter as tk

class Test_program():


    def __init__(self):


        self.r_root = tk.Tk()


        self.t_text = tk.StringVar()


        self.t_text.set(“Tkinter Change Label Text”)


        self.l_label = tk.Label(self.r_root, textvariable=self.t_text)


        self.b_button = tk.Button(self.r_root,


                                text=“Click here to change text written below”,


                                command=self.changeText)


        self.b_button.pack()


        self.l_label.pack()


        self.r_root.mainloop()


    def changeText(self):


        self.t_text.set(“Tkinter Change Label Text Example”)        


app=Test_program()

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Change-Label-Text-8.png" data-lazy- height="315" src="data:image/svg xml,” width=”563″>

Here you can view the resultant screen.

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Change-Label-Text-9.png" height="86" src="data:image/svg xml,” width=”279″>

By clicking on the button, it successfully updated the text from ‘Tkinter Change Label Text’ to ‘Tkinter Change Label Text Example.’

<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Tkinter-Change-Label-Text-10.png" height="82" src="data:image/svg xml,” width=”276″>

Conclusion:

Tkinter Label widgets are commonly used in applications to show text or images. You can change the label widget’s text property, color, background, and foreground colors using different methods. You can update the text of the label widget using a button and a function if you need to tweak or change it dynamically. And we’ve illustrated this concept with examples in this post.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/d014e3711df41253029f4d4199698df8?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content