[Python] Is there a way for empty tkinter frame objects to take up space?

Discussão em 'Python' iniciado por Stack, Julho 18, 2025.

  1. Stack

    Stack Membro Participativo

    I have an app which you can upload a picture into an image label object, but until I uploaded the picture, the image label just didn't take up space. So I put the image in a frame, but it still didn't take up space until I uploaded the image. Is there any way around this?

    Here is my code:

    from tkinter import filedialog
    from tkinter import *
    from PIL import Image, ImageTk

    ####################### FUNCTIONS ################################

    def upload_image():

    file_path = filedialog.askopenfilename()

    if file_path:
    img = Image.open(file_path)

    img_tk = ImageTk.PhotoImage(img)

    image_label.image = img_tk
    image_label.config(image=img_tk)



    ##################### GUI ##################################

    window = Tk()

    img_frame = Frame(window, width=500, height=500)
    image_label = Label(img_frame)

    upload_button = Button(window, text='Upload Image', command=upload_image)

    # Other UI things...



    img_frame.grid(column=3, row=2, columnspan=5, rowspan=10, padx=50)
    image_label.pack()
    upload_button.grid(column=1,row=1, padx=60, pady=60)


    window.mainloop()

    Continue reading...

Compartilhe esta Página