![]() |
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. Tkinter Label WidgetThe 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. An image can be added to a label just by converting it to PhotoImage. It requires the PhotoImage class which takes the image file as the argument. Then that image object is passed as the option of the image parameter of the Label widget. image_var = tk.PhotoImage(file="pathOfImageFilename") Steps to Update the Image of a Tkinter Label WidgetTo update the image of a Import moduleFirst step is to import the Tkinter module to create the Tkinter window and various widgets. import tkinter as tk Create Application WindowThen create the Tkinter application window which will contain all the necessary Tkinter widgets such as label image and the button to update the image. root = tk.Tk() Load the ImageLoad the images that will be displayed on the application window. This can be done by using the PhotoImage class which takes the path of the image file as parameter. image = tk.PhotoImage(file="path_to_image") Add WidgetsCreate the label widget, which is used to add the image on the Tkinter window. The Label() class takes two parameters, the first is the root window on which the label widget is to be displayed. The second is the image that we want to set as the label. label = tk.Label(root, image=image) Create a button which is used to update the image. When the user clicks the button, a function gets called which will update the image in the label widget. The ‘command’ option of the button class calls the function to update image. button = tk.Button(root, text="Update image", command=update_image_in_label) Update ImageThe image can be updated in the label widget by using the config() method. The option ‘image’ is used in the config() method assigning to a another PhotoImage variable. label.config(image=image_to_update) Run the ApplicationOnce everything is done, we can run the application window and update the image in a label with just a click of a button. root.mainloop() Updating Image of a Tkinter LabelTo 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.
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 ConclusionUpdating the image of a |
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |