Python Tkinter Entry pad text

10,012

Solution 1

I'm not aware a native way of adjusting the Entry's padding, but here's one way to get something like it. Make the entry's border invisible by giving it a FLAT style, and embed the entry in a Frame that acts as the entry's border. Then you can specify the padding by adjusting the entry's borderwidth. Example:

import tkinter as tk

root = tk.Tk()
frame = tk.Frame(root, borderwidth=5, relief=tk.SUNKEN)
frame.pack()
entry = tk.Entry(frame, borderwidth=15, relief=tk.FLAT)
entry.pack()
root.mainloop()

Result:

enter image description here

Solution 2

I had this problem, and after a bit of digging, I found out you can internally pad ttk's Entry widget:

import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()

ttk.Style().configure('pad.TEntry', padding='5 1 1 1')
ent = ttk.Entry(root, style='pad.TEntry')
ent.pack()

root.mainloop()

Where '5 1 1 1' is 'ipad_left ipad_top ipad_right ipad_bottom' in pixels (default is '1 1 1 1'). Using the 'TEntry' argument does change the style of every ttk.Entry however, so if you wanted to avoid that you could create a new style:

ttk.Style().configure('pad.TEntry', padding='5 1 1 1')
ent1 = ttk.Entry(root, style='pad.TEntry')
ent2 = ttk.Entry(root)

Where ent1 would have the extra padding and ent2 would not.

I figured out the 'padding' option existed by running print(Style().configure('TEntry')) which returns all the style options for TEntry (in this case it's just 'padding'). This brings up a problem though; ttk's widgets often don't have a lot of customization options readily available (TEntry is missing background, borderwidth, relief, etc) meaning you'd have to 'create' them. See these links for more information: ttk widgets || Using and customizing ttk styles || ttk.Entry information || (example) adding fieldbackground to ttk.Entry

Solution 3

using tkinter 8.6U

entry.config(padx = 15)
Share:
10,012
Programer Beginner
Author by

Programer Beginner

Apparently, this user prefers to keep an air of 𝓂𝓎𝓈𝓉𝑒𝓇𝓎 about them.

Updated on June 07, 2022

Comments

  • Programer Beginner
    Programer Beginner about 2 years

    How could I pad the entry widget so it does not start writing right at the border of the widget? Visually, having a little space from the entry widget border.

    My progress:

    entry_widget.bind('<FocusIn>', lambda f: entry_widget.insert(0, ' '))
    

    That adds an empty space when the user clicks the widget but there are multiple issue with this solution:

    1. When clicking out and clicking back in, it will add another space on top of the empty space or any text the user had filled in. I considered clearing the entry widget on FocusOut but that would also clear all the text that the user might have written.

    2. The user can delete the inserted space

    3. when getting the content, there is the additional space at the beginning. Though this is a small problem which can be solved by removing the first character of the content.

    And there might be more issues which I did not account for.

    I think the way where my code is heading is bad, therefore I am asking if anyone has any idea how to 'properly' pad the entry widget?

    • Mike - SMT
      Mike - SMT almost 6 years
      You could bind all keys to the entry widget to go to a function that first checks if there is a space at the beginning or not and if not put one.
    • jasonharper
      jasonharper almost 6 years
      Not quite what you asked for, but creating the Entry with justify='center' will keep the text away from the sides unless you actually need the full width to hold it all.
  • Mike - SMT
    Mike - SMT almost 6 years
    I like this. To bad it only works with a flat entry field.
  • Kevin
    Kevin almost 6 years
    If you're saying "it's a shame that you can't specify border styles for the Entry since it has to be flat", that's what the frame is for. By specifying its border style, you're effectively specifying the entry's border style.
  • Mike - SMT
    Mike - SMT almost 6 years
    Oh, I will have to play around with that then. I think having a little spacing would be nice in my app visually.
  • Programer Beginner
    Programer Beginner almost 6 years
    The user can still move to the front of the space using arrow keys. Also, the user can delete the space. The issue in number 2 still remains.
  • Programer Beginner
    Programer Beginner almost 6 years
    Very nice solution. with entry.pack(padx=N) I can customize the padding size.
  • Mike - SMT
    Mike - SMT almost 6 years
    @ProgramerBeginner As soon as they type anything else in front of the space a new space will be added. That said I understand what you are saying. I could probably try something else but I think your solutions is the answer.
  • danicotra
    danicotra about 3 years
    @giodamelio excuse me, I thought the correct syntax should be entry.pack_configure(padx=15) ... Can you tell me what the "U" you added stands for in "tkinter 8.6U"?