Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Tkinter Images and Icons


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:

1. Loading and Displaying Images in Labels

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).

Example: Displaying a .png Image


python
                        import 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()

                    


2. Using PIL to Load and Display .jpg Images

For .jpg and other formats, you need to use the PIL (Pillow) library. You can install it using pip install pillow.

Example: Displaying a .jpg Image


python
                        import 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()

                    

3. Displaying Images in Buttons

You can also display images on buttons using the image attribute.

Example: Button with an Image


python
                        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()

                    

Explanation:

  • compound=tk.LEFT: Places the image to the left of the text. Other options include tk.RIGHT, tk.TOP, and tk.BOTTOM.


4. Displaying Images in Canvas

You can display images on a Canvas widget using the create_image method.

Example: Displaying an Image on a Canvas


python
                        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()

                    


5. Using Icons for the Tkinter Window

You can set an icon for your Tkinter window using the iconphoto method.

Example: Setting a Window Icon


python
                        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()

                    

Summary

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.







Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java