How to Set a Background Image in Python Tkinter?

I recently faced a challenge while developing a Tkinter application for my clients, They wanted a visually appealing background image that would captivate users and align with their brand identity. After extensive research and experimentation, I discovered the most effective methods to set background images in Tkinter. In this tutorial, I will explain how to set a background image in your Python Tkinter with examples.

Set a Background Image in Python Tkinter

To display images in Tkinter widgets such as labels, buttons, and canvases, we use the PhotoImage class. This class is part of the Tkinter package and allows us to load and manipulate image files.

Here’s a simple example of how to create a PhotoImage object:

import tkinter as tk

window = tk.Tk()
background_image = tk.PhotoImage(file="path/to/your/image.png")

In this code snippet, we import the Tkinter module and create a window using tk.Tk() , and then create a PhotoImage object by specifying the file path of our desired background image.

Read How to Create a Python Tkinter Panel with Search Functionality?

Method 1: Use the Label Widget

One simple approach to setting a background image in Tkinter is to use the Label widget. Here’s how you can do it:

from PIL import Image, ImageTk
import tkinter as tk

root = tk.Tk()
image = Image.open("path/to/your/image.png")
photo = ImageTk.PhotoImage(image)

label = tk.Label(root, image=photo)
label.pack()

root.mainloop()

You can look at the output in the screenshot below.

Set a Background Image in Python Tkinter

In this example, we create a Label widget called background_label and assign our background_image to its image parameter. We then use the place() geometry manager to position the label at coordinates (0, 0) and set its relative width and height to 1, ensuring it covers the entire window.

Check out How to Create Scrollable Frames with Python Tkinter?

Method 2: Use the Canvas Widget

Another effective method to set a background image is by utilizing the Canvas widget. The Canvas allows for more flexibility in terms of image positioning and layering. Here’s an example:

from PIL import Image, ImageTk
import tkinter as tk

window = tk.Tk()
window.title("My Tkinter App")
window.geometry("800x600")

canvas = tk.Canvas(window, width=800, height=600)
canvas.pack(fill="both", expand=True)

# Load image with Pillow instead of Tkinter's PhotoImage
image_path = r"C:path/to/your/image.png"
image = Image.open(image_path)
background_image = ImageTk.PhotoImage(image)

canvas.create_image(0, 0, image=background_image, anchor="nw")

window.mainloop()

You can look at the output in the screenshot below.

How to Set a Background Image in Python Tkinter

In this code, we create a Canvas widget with the desired width and height. We use the pack() geometry manager to make the canvas fill the entire window. Then, we use the create_image() method of canvas to place our background image at coordinates (0, 0) with the “nw” (northwest) anchor, ensuring the image aligns with the top-left corner of the canvas.

Read How to Create a Text Box in Python Tkinter?

Tips for Using Background Images

When using background images in your Tkinter application, consider the following tips:

  • Choose an image with appropriate dimensions that match your application’s window size to avoid stretching or distortion.
  • Ensure the image file format is supported by Tkinter (e.g., PNG, GIF, PPM).
  • Optimize the image file size to minimize loading time and improve application performance.
  • Consider the contrast and readability of text and other widgets placed on top of the background image.

Check out How to Create Checkboxes in Python Tkinter?

Handle Dynamic-Window Resizing

If your Tkinter application allows window resizing, you may want to ensure that the background image scales proportionally with the window size. Here’s an example of how to achieve this using the Canvas widget:

from PIL import Image, ImageTk
import tkinter as tk

def resize_background(event):
    global background_image, bg_image_tk
    
    # Resize image using PIL
    resized_image = bg_image.resize((event.width, event.height), Image.Resampling.LANCZOS)
    bg_image_tk = ImageTk.PhotoImage(resized_image)

    canvas.delete("background")
    canvas.create_image(0, 0, image=bg_image_tk, anchor="nw", tags="background")

window = tk.Tk()
window.title("My Tkinter App")
window.geometry("800x600")

canvas = tk.Canvas(window, width=800, height=600)
canvas.pack(fill="both", expand=True)

# Load image with Pillow
image_path = r"C:\Users\Public\code\images\flower.png"
bg_image = Image.open(image_path)
bg_image_tk = ImageTk.PhotoImage(bg_image)

canvas.create_image(0, 0, image=bg_image_tk, anchor="nw", tags="background")

canvas.bind("<Configure>", resize_background)

window.mainloop()

You can look at the output in the screenshot below.

Set a Background Image in Python Tkinter dynamic window size

In this code, we define a resize_background() function that gets called whenever the window is resized. It zooms and subsamples the background image based on the new window dimensions, deletes the previous background image from the canvas, and creates a new image with the updated size. We bind the <Configure> event to the canvas, which triggers the resize_background() function whenever the window size changes.

Read Python Tkinter TreeView

Conclusion

In this tutorial, I have explained how to set a background image in your Python Tkinter. I discussed two methods to set background images they are using a label widget and using canvas widget. I also discussed tips for using background images and how to handle dynamic window resizing.

You may read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.