1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How can I make a square Tkinter Entry widget if width only accepts integers?

Discussão em 'Python' iniciado por Stack, Novembro 25, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to create a square tk.Entry widget in Tkinter, but the width parameter only accepts integers which represent a number of characters instead of pixels.

    Setting width=2 makes the box too narrow, while width=3 makes it too wide.

    import tkinter as tk

    root = tk.Tk()

    entry2 = tk.Entry(root, width=2, bd=3)
    entry2.pack(pady=5, anchor="w")

    entry3 = tk.Entry(root, width=3, bd=3)
    entry3.pack(pady=5, anchor="w")

    root.mainloop()


    [​IMG]

    Trying to use a decimal value causes an an error.

    import tkinter as tk

    root = tk.Tk()

    entry_decimal = tk.Entry(root, width=2.5, bd=3)
    entry_decimal.pack(pady=5, anchor="w")


    root.mainloop()


    _tkinter.TclError: expected integer but got "2.5"


    According to TkDocs, the width for an Entry widget is based on average character width, so it's not pixel-precise and varies by font.


    The size of the entry in characters. [...] For proportional fonts, the physical length of the widget will be based on the average width of a character times the value of the width option.
    ​

    Is there any way to make a square-shaped Entry widget with equal width and height?

    Continue reading...

Compartilhe esta Página