[Python] Can label or button act as parent in tkinter?

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

  1. Stack

    Stack Membro Participativo

    I always thought that only frame and other container elements can be parent, but recently when I tried the below code, it seemed to work perfectly without any error.

    import os
    import tkinter as tk
    from PIL import Image, ImageTk

    BASE_DIR = os.path.dirname(os.path.abspath(__file__))

    main = tk.Tk()
    main.title("Main Window")
    main.config(bg="#E4E2E2")
    main.geometry("700x400")


    frame = tk.Frame(master=main)
    frame.config(bg="#d1c9c9")
    frame.pack()


    label2 = tk.Label(master=frame, text="Password")
    label2.config(bg="#d1c9c9", fg="#000")
    label2.pack(side=tk.TOP)


    button = tk.Button(master=frame, text="Submit")
    button.config(bg="#161515", fg="#ffffff")
    button.pack(side=tk.TOP)

    entry1 = tk.Entry(master=button)
    entry1.config(bg="#fff", fg="#000")
    entry1.pack(side=tk.TOP)


    main.mainloop()


    The entry seems to be appearing inside the button when i try it on my linux PC. So, is it fine to use labels, button widgets and others as parents? Will it cause any issues on other OS?

    Continue reading...

Compartilhe esta Página