Change label text in Tkinter through variables in python

To change the text of label in Tkinter using variables, you can untilize the stringVar class . This allows you to bind a variable to the label, and any changes to the variable will automatically update the label’s text.

CODE

import tkinter as tk

def update_label():

label_text.set(“Text updated!”)

root = tk.TK()

label_text  = tk.StringVar()

label_text.set(“Initial text”)

label = tk.Label(root, textvariable=label_text)

label.pack()

button = tk.button(root, text=”update Label”, command=update_label)

button.pack()

root.mainloop()

OUTPUT:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top