[Python] Tkinter widget formatting problems

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

  1. Stack

    Stack Membro Participativo

    This is my code:

    from tkinter import filedialog, ttk
    from tkinter import *
    from PIL import Image, ImageTk
    import numpy as np


    ########## GLOBAL VARIABLES ########
    img_tk = None


    ####################### BACKEND ################################

    # Upload Image Button
    def upload_image():
    global img_tk
    file_path = filedialog.askopenfilename()

    if file_path:
    img = Image.open(file_path)

    img_tk = ImageTk.PhotoImage(img)
    if img_tk.width() < 1200 or img_tk.height() < 600:
    image_label.image = img_tk
    image_label.config(image=img_tk)



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

    window = Tk()

    img_frame = Frame(window)
    image_label = Label(window, width=1200, height=600)

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

    text_size_label = Label(window, text="Text size:", font=('calibre',12, 'bold'))
    text_size = ttk.Combobox(window, font=('calibre',12, 'normal'), values=list(np.arange(8, 51, step=7)))

    rotation_label = Label(window, text="Rotation:", font=('calibre',12, 'bold'))
    rotation = Entry(window, font=('calibre',12, 'normal'))

    opacity_label = Label(window, text="Opacity:", font=('calibre',12, 'bold'))
    opacity = Scale(window, from_=0, to=100, orient='horizontal')


    # Placements
    img_frame.grid(column=3, row=2, columnspan=5, rowspan=10, padx=50)
    image_label.pack([![enter image description here][1]][1])
    upload_button.grid(column=1,row=1, padx=60, pady=60)
    text_size_label.grid(column=1, row=2)
    text_size.grid(row=2, column=2)
    rotation_label.grid(row=3, column=1)
    rotation.grid(row=3, column=2)
    opacity_label.grid(row=4, column=1)
    opacity.grid(row=4, column=2)

    window.mainloop()


    So there is an upload button which uploads an image to my Frame object. Now, the problem is before I upload my image, my formatting goes all wrong like this: [​IMG]

    and when I upload my image, it formats right:

    [​IMG]

    Also, before uploading the image, the app almost covers the whole screen and after uploading, doesn't cover the whole screen.

    Continue reading...

Compartilhe esta Página