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.
# 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:
Click on the ‘Update image’ button. The image on the Label changes on clicking the button as below,
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.
Contact Us