How to change Tkinter label text on button press?

TkinterGUI-ProgrammingPython

Most often, Tkinter Label widgets are used in the application to display the text or images. We can configure the label widget such as its text property, color, background or foreground color using the config(**options) method.

If you need to modify or change the label widget dynamically, then you can use a button and a function to change the text of the label widget.

Example

# Import the required libraries
from tkinter import *

# Create an instance of tkinter frame or window
win = Tk()

# Set the size of the tkinter window
win.geometry("700x350")

# Define a function update the label text
def on_click():
   label["text"] = "Python"
   b["state"] = "disabled"

# Create a label widget
label = Label(win, text="Click the Button to update this Text",
font=('Calibri 15 bold'))
label.pack(pady=20)

# Create a button to update the label widget
b = Button(win, text="Update Label", command=on_click)
b.pack(pady=20)

win.mainloop()

Output

When you run the above code, it will show a label text and a button in the window.

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

raja
Published on 06-Aug-2021 06:46:52
Advertisements