Tkinter resize background image to window size

Trying to customize the background for my tkinter window. I have a square background image that fades to black at the edges, and then the main window has a black background. The image is placed on top of the background, and if the window is wider than the tall one, the image is centered on a black background and everything looks very beautiful.

However, when the window is smaller than the image in width and height, it puts the center of the image in the center of the window, so you do not see the whole image, and it looks a bit strange. Is there a way to resize the image so that if the largest width and height of the window is smaller than the image, the image is adjusted to this size while maintaining the aspect ratio.

Say the background image is 600x600 :

  • In the 800x400 window 800x400 image does not resize and centers itself vertically.
  • In the 500x400 window 500x400 image is resized to 500x500 and still centers itself vertically.
  • In the 400x900 window 400x900 image does not resize and centers itself horizontally.

The centering function already exists, I just need the resize functions.

I currently have:

 from tkinter import * root = Tk() root.title("Title") root.geometry("600x600") root.configure(background="black") background_image = PhotoImage(file="Background.gif") background = Label(root, image=background_image, bd=0) background.pack() root.mainloop() 

Not sure if there is a way to do this in tkinter? Or perhaps I would write my own function that resizes the image to fit the window, however the image should change relatively smoothly and quickly if the user resizes the window at any point.

+7
source share
2 answers

This is an example application that uses Pillow to resize an image on a label as the label resizes:

 from tkinter import * from PIL import Image, ImageTk root = Tk() root.title("Title") root.geometry("600x600") root.configure(background="black") class Example(Frame): def __init__(self, master, *pargs): Frame.__init__(self, master, *pargs) self.image = Image.open("./resource/Background.gif") self.img_copy= self.image.copy() self.background_image = ImageTk.PhotoImage(self.image) self.background = Label(self, image=self.background_image) self.background.pack(fill=BOTH, expand=YES) self.background.bind('<Configure>', self._resize_image) def _resize_image(self,event): new_width = event.width new_height = event.height self.image = self.img_copy.resize((new_width, new_height)) self.background_image = ImageTk.PhotoImage(self.image) self.background.configure(image = self.background_image) e = Example(root) e.pack(fill=BOTH, expand=YES) root.mainloop() 

Here's how it works, using Lenna as an example:

enter image description here

+11
source

I modified the code above so that it is not in the class

 #!/usr/bin/python3.5 from tkinter import * from tkinter import ttk from PIL import Image, ImageTk root = Tk() root.title("Title") root.geometry('600x600') def resize_image(event): new_width = event.width new_height = event.height image = copy_of_image.resize((new_width, new_height)) photo = ImageTk.PhotoImage(image) label.config(image = photo) label.image = photo #avoid garbage collection image = Image.open('image.gif') copy_of_image = image.copy() photo = ImageTk.PhotoImage(image) label = ttk.Label(root, image = photo) label.bind('<Configure>', resize_image) label.pack(fill=BOTH, expand = YES) root.mainloop() 
+4
source

Source: https://habr.com/ru/post/970393/


All Articles