- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to resize the background image to window size in Tkinter?
In order to work with images, Python Library provides Pillow or PIL package that enables applications to import images and perform various operations on them.
Let us suppose that we want to resize an image dynamically to its window. In such case, we have to follow these steps −
Open the Image in the Tkinter Application.
Create a Canvas Widget and use create_image(**options) to place the loaded image in the canvas.
Define a function to resize the loaded image.
Bind the function with the parent window configuration.
# Import the required libraries from tkinter import * from PIL import ImageTk, Image # Create an instance of Tkinter Frame win = Tk() # Set the geometry of Tkinter Frame win.geometry("700x450") # Open the Image File bg = ImageTk.PhotoImage(file="tutorialspoint.png") # Create a Canvas canvas = Canvas(win, width=700, height=3500) canvas.pack(fill=BOTH, expand=True) # Add Image inside the Canvas canvas.create_image(0, 0, image=bg, anchor='nw') # Function to resize the window def resize_image(e): global image, resized, image2 # open image to resize it image = Image.open("tutorialspoint.png") # resize the image with width and height of root resized = image.resize((e.width, e.height), Image.ANTIALIAS) image2 = ImageTk.PhotoImage(resized) canvas.create_image(0, 0, image=image2, anchor='nw') # Bind the function to configure the parent window win.bind("<Configure>", resize_image) win.mainloop()
Running the above code will display a window containing an image which can be dynamically resized.
- How to resize an image using Tkinter?
- How can I resize the root window in Tkinter?
- Resize the Tkinter Listbox widget when the window resizes
- How to use an image for the background in tkinter?
- PyTorch – How to resize an image to a given size?
- How to Resize Browser Window in WebDriver?
- How to set a Tkinter window to a constant size?
- How to resize image in PHP?
- Dynamically Resize Buttons When Resizing a Window using Tkinter
- How to Set a Tkinter Window with a Constant Size?
- How to resize an Image C#?
- How to explicitly resize frames in tkinter?
- How to resize Image in Android App?
- How To Dynamically Resize Button Text in Tkinter?