![]() |
Python Tkinter supports various image formats such as PNG, JPEG, GIF, and BMP. Images are handled in Tkinter using the PhotoImage and BitmapImage classes and the Pillow Library. The PhotoImage class is more commonly used due to its support for color images, whereas BitmapImage is limited to monochrome bitmaps. Adding images in Tkinter can be done by using various Tkinter Widgets, such as Label, Buttons, and Canvas. Steps to add Image in TkinterLet us see step by step how we can add images in a Tkinter application window using Tkinter widgets. Import Modules and ClassesThe first step is to import the Tkinter module and various other classes as per your requirements such as PhotoImage or BitmapImage. import tkinter
from tkinter import PhotoImage Create Tkinter WindowNext step is to create a basic Tkinter window where all the widgets as well as the image will be displayed. parent = tk.Tk()
parent.title("Application Title") Load the ImageNow, load the image to be displayed in the Tkinter window. We can load the image using PhotoImage or Python Pillow Library. Each have a quite similar method to load the image. We can see it in detail in below examples. image = PhotoImage(file="gfg.png") Adding Image to LabelTo display the image in Tkinter window, we will add the image to the Label widget. It takes two parameter, one is the root window on which we want the widget to display and the other is the image object in which we loaded the image in the previous step. image_label = tkinter.Label(root, image)
image_label.pack() Run the ApplicationThe last step to to execute our code and see the image displayed in the Tkinter window. parent.mainloop() Adding Image in TkinterLet us see each method, one by one in detail to add the image in Tkinter window using Tkinter Label widget. Using PhotoImageThe PhotoImage class of Tkinter is commonly used to create and manipulate images. We can load the image in Tkinter by passing the path of the image file to the file parameter of the PhotoImage() class.
Output: ![]() Adding Image in Tkinter using PhotoImage Using PIL (Python Imaging Library)The PIL (Python Imaging Library) provides extensive functionality for opening, manipulating, and saving many different image file formats such as PNG and JPEG. We can load the image using the PIL module’s Image.open() function and the ImageTk.PhotoImage() dunction which will take the image path as the parameter.
Output: ![]() Adding Image in Tkinter using PIL module Adding Image to ButtonWe can also add Images in Tkinter using the Button Widget. Like previous method, instead of adding image to the label, here we will add image to the button widget. We can do this by using the Button() class of the Tkinter module, which will take the root window and the image obeject as the parameter.
Output: ![]() Adding Image to a Button in Tkinter ConclusionIntegrating images into Tkinter applications enhances their visual appeal and user experience. Whether using the PhotoImage class, PIL library, or Canvas widget, you have multiple options to incorporate images seamlessly into your Tkinter projects. Experiment with different methods and explore additional functionalities to create dynamic and engaging graphical interfaces. |
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 17 |