3

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?

Programer Beginner
  • 1,377
  • 6
  • 21
  • 47
  • 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. – Mike - SMT Aug 13 '18 at 12:55
  • 1
    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. – jasonharper Aug 13 '18 at 13:11

5 Answers5

13

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

Kevin
  • 74,910
  • 12
  • 133
  • 166
  • I like this. To bad it only works with a `flat` entry field. – Mike - SMT Aug 13 '18 at 13:09
  • 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. – Kevin Aug 13 '18 at 13:16
  • Oh, I will have to play around with that then. I think having a little spacing would be nice in my app visually. – Mike - SMT Aug 13 '18 at 13:22
  • Very nice solution. with `entry.pack(padx=N)` I can customize the padding size. – Programer Beginner Aug 13 '18 at 15:00
7

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

Cryden
  • 75
  • 1
  • 6
1

using tkinter 8.6U

entry.config(padx = 15)
giodamelio
  • 5,465
  • 14
  • 44
  • 72
MichaelDL
  • 57
  • 4
  • @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"? – danicotra Apr 26 '21 at 22:49
0

Here is a simple example of how you can force a space to start with and then with a bind keep the space while the user types.

import tkinter as tk

root = tk.Tk()

def check_for_space(event):
    if entry.get() == "":
        entry.insert(0, " ")
    elif entry.get()[0] != " ":
        entry.insert(0, " ")
    else:
        pass

entry = tk.Entry(root)
entry.insert(0, " ")
entry.pack()
entry.bind("<Key>", check_for_space)

root.mainloop()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • 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 Aug 13 '18 at 14:10
  • @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. – Mike - SMT Aug 13 '18 at 16:14
0

Try this Custom made entry

from tkinter import Entry,Tk

class MyEntry(Entry):
    def __init__(self,root,placeholder,*args,**kw):
        self.root = root
        self.placeholder = placeholder
        Entry.__init__(self,self.root,*args,**kw)

        self.add()

        self.bind("<FocusIn>",self.focusin)
        self.bind("<FocusOut>",self.focusout)

    def add(self):
        if self.get()=='':
            self.insert(0,self.placeholder)

    def focusin(self,event=None):
        if self.get()=='' or self.get()==self.placeholder:
            self.delete(0,END)

    def focusout(self,event=None):
        if self.get()=='':
            self.add()

root = Tk()

a = MyEntry(root,placeholder='<Name>')
a.pack()
b = MyEntry(root,placeholder='<Contact>')
b.pack()