Changing Tkinter Label Text Dynamically using Label.configure()


The Label widget in tkinter is generally used to display text as well as image. Text can be added in a Label widget by using the constructor Label(root, text= "this is my text"). Once the Label widget is defined, you can pack the Label widget using any geometry manager.

If you want to configure the Label widget, you can use the configure() property. The configure() method allows you to edit the text as well other properties of the Label widget dynamically.

Example

Let us take an example to understand how we can dynamically change the tkinter label text using the configure() method. In this example, we will create a Label text widget and a button to update the text of the label widget.

# Import the required library
from tkinter import *

# Create an instance of tkinter frame or widget
win = Tk()
win.geometry("700x350")

def update_text():
   # Configuring the text in Label widget
   label.configure(text="This is updated Label text")

# Create a label widget
label=Label(win, text="This is New Label text", font=('Helvetica 14 bold'))
label.pack(pady= 30)

# Create a button to update the text of label widget
button=Button(win, text= "Update", command=update_text)
button.pack()

win.mainloop()

Output

Running the above code will display a window with a label widget containing some text and a button.

When you click the button, it will update the text.

Updated on: 22-Dec-2021

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements