1

I'll preface this with, I am very new to programming. I'm trying to resize my tkinter window "background_app" automatically to any photo that I import into the app. I've seen threads with examples, but they usually create a new class. I'm not at the level of creating a new class yet and I'm wondering if I can do the task without creating a new class. Below is my code with the size currently set to "600x600." Does anyone have any suggestions? Thank you.

import tkinter as tk
from PIL import Image, ImageTk

def main():
    buttons()

def buttons():
    background_app = tk.Tk()
    background_app.title(input("Title? "))
    path = r'C:\Users\Kate\Desktop\Rockphoto.jpg'
    img = ImageTk.PhotoImage(Image.open(path))   #Creates Tkinter photo image
    background_app.geometry("600x600")

    route_image = tk.Label(background_app, image = img) 
    route_image.pack() #Packs widgets in rows or columns

    background_app.mainloop()
stovfl
  • 14,998
  • 7
  • 24
  • 51
kmyghty
  • 11
  • 1
  • Just get rid of the `.geometry()` line. Sizing the window to fit the contents is the default behavior of Tkinter. – jasonharper May 23 '20 at 23:17
  • ***resize ... window ... to any photo***: This approach will only work as long the images are not larger then your screen. Does this answer your question? [Tkinter resize background image to window size](https://stackoverflow.com/a/24074612/7414759) – stovfl May 24 '20 at 11:24
  • Thanks for the responses. Getting rid of the .geometry() worked! I'll try it for a few more images. – kmyghty May 25 '20 at 19:42

1 Answers1

2

You can use PIL to get the image dimensions

Reference here

image = PIL.Image.open("sample.png")

Get dimensions

width, height = image.size

Then you can feed this into the geometry. Hope this helps :)

Leo Gaunt
  • 711
  • 1
  • 8
  • 29
  • I'm struggling with this one. I printed the image.size and it gives me width and height so that works. But when I plug it into geometry is says "bad geometry specifier" – kmyghty May 25 '20 at 19:53