Updating Image of a Tkinter Label

To update image in a Label, an event has to happen. A button is created and on clicking the button, the image in the Label gets updated to another image. A function is assigned to the ‘command’ option of the Button widget to update the image of the Label at a button click. Two images are needed, where one image is initially displayed in the Label widget, and the another image is updated in the Label on clicking the button.

Python
# importing tkinter
import tkinter as tk

# Function to update the image in label
def update_image_in_label():
    label_pic.config(image=image_2)

# tkinter application window
root = tk.Tk()
root.title("w3wiki")
root.geometry("400x200")
root.config(bg="green")

# Converting image to PhotoImage variables
image_1 = tk.PhotoImage(file="books_picture.png").subsample(2, 2)
image_2 = tk.PhotoImage(file="w3wiki-logo.png")

# Label widget with image
label_pic = tk.Label(root, image=image_1)
label_pic.pack(pady=15)

# Button widget
update_button = tk.Button(root, text="Update image",
                          command=update_image_in_label, 
                          bg="black", fg="white", 
                          font=("Arial", 15))
update_button.pack()

# Run application
root.mainloop()

Output:

Output window of Label widget

Click on the ‘Update image’ button. The image on the Label changes on clicking the button as below,

Image updated in Label widget in Tkinter Window

How to Update the Image of a Tkinter Label Widget?

Tkinter is a GUI-creating module in Python that is used to create many applications. It has many useful widgets such as Label, Checkbutton, Button, Checkbox, and more. In this article, we will discuss how we can update an image from the Label widget in Tkinter in Python.

Similar Reads

Tkinter Label Widget

The Label is one of the widgets in Tkinter. It is used to display text in the GUI window. An image can also be displayed using the Label widget. Also, a label can contain both text and image simultaneously in the window....

Steps to Update the Image of a Tkinter Label Widget

To update the image of a Label widget, follow these steps:...

Updating Image of a Tkinter Label

To update image in a Label, an event has to happen. A button is created and on clicking the button, the image in the Label gets updated to another image. A function is assigned to the ‘command’ option of the Button widget to update the image of the Label at a button click. Two images are needed, where one image is initially displayed in the Label widget, and the another image is updated in the Label on clicking the button....

Conclusion

Updating the image of a Label widget in Tkinter is a straightforward process that involves loading a new image and updating the widget’s image property. This capability is essential for creating dynamic and interactive applications. By understanding the key aspects of Tkinter’s image handling and maintaining proper image references, you can effectively manage and update images in your applications....

Contact Us