Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to resize the background image to window size in Tkinter?
In order to work with images, Python provides the Pillow (PIL) package that enables applications to import and manipulate images. When creating Tkinter applications, you often want the background image to resize automatically with the window.
To resize a background image dynamically to match the window size, follow these steps −
Import the required libraries (
tkinterandPIL).Create a Canvas widget and use
create_image()to place the image.Define a function to resize the image based on window dimensions.
Bind the resize function to the window's
<Configure>event.
Example
Here's a complete example that creates a resizable background image ?
# Import the required libraries
from tkinter import *
from PIL import ImageTk, Image
# Create an instance of Tkinter Frame
win = Tk()
win.title("Resizable Background Image")
# Set the initial geometry
win.geometry("700x450")
# Load the original image
original_image = Image.open("background.jpg") # Replace with your image path
# Create a Canvas that fills the entire window
canvas = Canvas(win)
canvas.pack(fill=BOTH, expand=True)
# Function to resize the image when window is resized
def resize_image(event):
# Get the new window dimensions
new_width = event.width
new_height = event.height
# Resize the original image to match window size
resized_image = original_image.resize((new_width, new_height), Image.LANCZOS)
# Convert to PhotoImage
photo = ImageTk.PhotoImage(resized_image)
# Clear the canvas and add the resized image
canvas.delete("all")
canvas.create_image(0, 0, image=photo, anchor='nw')
# Keep a reference to prevent garbage collection
canvas.image = photo
# Initial image setup
resize_image(type('obj', (object,), {'width': 700, 'height': 450}))
# Bind the resize function to canvas configuration changes
canvas.bind("<Configure>", resize_image)
win.mainloop()
How It Works
The <Configure> event triggers whenever the canvas size changes. The resize_image() function ?
Gets the new canvas dimensions from the event object
Resizes the original image using
Image.LANCZOSfor better qualityClears the canvas and draws the new resized image
Maintains a reference to prevent garbage collection
Key Points
Use
Image.LANCZOSinstead of deprecatedImage.ANTIALIASStore a reference to the PhotoImage (
canvas.image = photo) to prevent it from being garbage collectedBind to the canvas
<Configure>event rather than the window for more accurate sizingUse
canvas.delete("all")to clear previous images before drawing new ones
Alternative Approach with Label
You can also use a Label widget as a background ?
from tkinter import *
from PIL import ImageTk, Image
def resize_background(event):
# Resize image to label size
image = Image.open("background.jpg")
resized = image.resize((event.width, event.height), Image.LANCZOS)
photo = ImageTk.PhotoImage(resized)
# Update label image
bg_label.config(image=photo)
bg_label.image = photo # Keep reference
root = Tk()
root.geometry("800x600")
# Create background label
bg_label = Label(root)
bg_label.place(x=0, y=0, relwidth=1, relheight=1)
# Bind resize event
bg_label.bind("<Configure>", resize_background)
root.mainloop()
Conclusion
Use the Canvas approach with <Configure> event binding for dynamic background image resizing. Always keep a reference to the PhotoImage object and use Image.LANCZOS for better image quality when resizing.
