0

I have the following tkinter example:

from PIL import Image, ImageTk
import tkinter as tk

root = tk.Toplevel()

container_frame = tk.Frame(master=root)
container_frame.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
top_frame = tk.Frame(container_frame, background='red')
middle_frame = tk.Frame(container_frame, background='green')
bottom_frame = tk.Frame(container_frame, background='blue')

top_frame.grid(row=0, column=0, sticky='NSEW')
middle_frame.grid(row=1, column=0, sticky='NSEW')
bottom_frame.grid(row=2, column=0, sticky='NSEW')
container_frame.grid_columnconfigure(0, weight=1)
container_frame.grid_rowconfigure(0, weight=1, uniform='container_frame')
container_frame.grid_rowconfigure(1, weight=7, uniform='container_frame')
container_frame.grid_rowconfigure(2, weight=2, uniform='container_frame')

image = Image.open('some_image.png')
photo_image = ImageTk.PhotoImage(image)
label = tk.Label(top_frame, image=photo_image, background='yellow', anchor='w')
label.image = photo_image
label.pack(expand=1, side='left')

root.geometry('1280x720')
root.mainloop()

Unfortunately the image within the label has not been shrunk to fit the top_frame correctly. Why is this? Furthermore, when I adjust the size of the window the label (and thus image) is not resized at all. Again, why is this?

Thanks for any help.

ajrlewis
  • 2,968
  • 3
  • 33
  • 67
  • Label never resize image - you have to do it on your own. You have to use `PIL` to generate image with new size. – furas Jul 31 '19 at 14:18
  • example how to use `bind('')` to run function when image changed size - and this function use PIL to generate image with new size: [furas/python-examples/tkinter/resize-image-in-background/](https://github.com/furas/python-examples/blob/master/tkinter/resize-image-in-background/main.py) – furas Jul 31 '19 at 14:21

1 Answers1

2

Unfortunately the image within the label has not been shrunk to fit the top_frame correctly. Why is this?

It is because that is not how tkinter works. It will not automatically grow and shrink images for you. It will grow and shrink the label, but not the image inside. You will have to write code to do that using an external library such as Pillow.

Furthermore, when I adjust the size of the window the label (and thus image) is not resized at all. Again, why is this?

As I wrote earlier, the image will never change. As for why the label doesn't change, it's because you've configured it not to change.

Consider this line of code:

label.pack(expand=1, side='left')

The expand option says that any extra space is given to the label, which tkinter does. However, by leaving the fill option as the default value, you've instructed tkinter to not expand the label to fill the space allocated to it.

If you want the label to grow, add fill='both'.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thanks for your detailed answer -- helps a lot. Do you know a good link for an algorithm to "grow and shrink" an image? – ajrlewis Jul 31 '19 at 20:09
  • @alex_lewis: this question deals with a background for a window, but the same technique would work for a label: https://stackoverflow.com/questions/24061099/tkinter-resize-background-image-to-window-size – Bryan Oakley Jul 31 '19 at 20:15