Working with images and icons in Tkinter involves loading image files and displaying them within
various widgets such as Label
, Button
, and Canvas
. Here's a
step-by-step guide on how to work with images and icons in Tkinter:
Tkinter supports displaying images using the PhotoImage
class for .png
and
.gif
formats. For other formats like .jpg
, you can use the
PIL
library (Python Imaging Library).
.png
Imageimport tkinter as tk # Create the main window root = tk.Tk() root.title("Image in Label Example") root.geometry("300x300") # Load an image image = tk.PhotoImage(file="path/to/your/image.png") # Create a Label to display the image label = tk.Label(root, image=image) label.pack(pady=20) # Keep a reference to the image to prevent it from being garbage collected label.image = image # Run the application root.mainloop()
.jpg
ImagesFor .jpg
and other formats, you need to use the PIL
(Pillow) library. You
can install it using pip install pillow
.
.jpg
Imageimport tkinter as tk from PIL import Image, ImageTk # Create the main window root = tk.Tk() root.title("JPG Image in Label Example") root.geometry("300x300") # Load an image using PIL image = Image.open("path/to/your/image.jpg") photo = ImageTk.PhotoImage(image) # Create a Label to display the image label = tk.Label(root, image=photo) label.pack(pady=20) # Keep a reference to the image to prevent it from being garbage collected label.image = photo # Run the application root.mainloop()
You can also display images on buttons using the image
attribute.
import tkinter as tk # Create the main window root = tk.Tk() root.title("Button with Image Example") root.geometry("300x200") # Load an image image = tk.PhotoImage(file="path/to/your/image.png") # Create a Button with an image button = tk.Button(root, image=image, text="Click Me", compound=tk.LEFT) button.pack(pady=20) # Keep a reference to the image to prevent it from being garbage collected button.image = image # Run the application root.mainloop()
compound=tk.LEFT
: Places the image to the left of the text. Other options include
tk.RIGHT
, tk.TOP
, and tk.BOTTOM
.
You can display images on a Canvas
widget using the create_image
method.
import tkinter as tk # Create the main window root = tk.Tk() root.title("Canvas with Image Example") root.geometry("400x300") # Create a Canvas widget canvas = tk.Canvas(root, width=300, height=200, bg="white") canvas.pack(pady=20) # Load an image image = tk.PhotoImage(file="path/to/your/image.png") # Add the image to the canvas canvas.create_image(150, 100, image=image) # Keep a reference to the image to prevent it from being garbage collected canvas.image = image # Run the application root.mainloop()
You can set an icon for your Tkinter window using the iconphoto
method.
import tkinter as tk # Create the main window root = tk.Tk() root.title("Window Icon Example") root.geometry("300x200") # Load an icon image icon = tk.PhotoImage(file="path/to/your/icon.png") # Set the window icon root.iconphoto(False, icon) # Run the application root.mainloop()
By following these steps, you can effectively work with images and icons in Tkinter. You can display images in Label
, Button
, and Canvas
widgets and set custom window icons. Using the PhotoImage
class and the PIL
library allows you to work with various image formats and enhance the visual appeal of your Tkinter applications.