[Python] Why is the tk.Entry Widget not shown?

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

  1. Stack

    Stack Membro Participativo

    Why does is the Entry Widget with yellow background not shown?

    import tkinter as tk

    root = tk.Tk()
    entryText1 = tk.Entry(root, bg='yellow')
    entryText1.delete(0,tk.END)
    entryText1.insert(0,'Enter Upper Threshold:')
    entryText1.pack()

    root.focus_force()
    root.mainloop()


    It exists and when i click on it, the yellow color is visible. I am using a Mac with Python 3.12.4

    I played around a bit and if i just add a button afterwards, the Entry is shown correctly.

    import tkinter as tk

    root = tk.Tk()
    entryText1 = tk.Entry(root, bg='yellow', fg="red")
    entryText1.delete(0,tk.END)
    entryText1.insert(0,'Enter Upper Threshold:')
    entryText1.pack()

    button= tk.Button(root)
    button.pack()

    root.focus_force()
    root.mainloop()



    If you put the Entry inside a Frame with a defined width and height it works again.

    import tkinter as tk

    root = tk.Tk()

    input_frame= tk.Frame(root, width=200, height=200)
    input_frame.pack()

    entry = tk.Entry(input_frame, bg='yellow', fg="red") # Width of entry widget
    entry.delete(0,tk.END)
    entry.insert(0, 'Enter Upper Threshold:')
    entry.pack()
    root.focus_force()
    root.mainloop()


    If you create a Frame without specifying a height AND width, the Entry is not visible again.

    So the real question is: Why is tkinter not consistent or what exactly am I missing. Is there a way to make the Entry visible after packing it?

    Continue reading...

Compartilhe esta Página