Programer Beginner
Programer Beginner

Reputation: 1419

Python Tkinter Entry pad text

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?

Upvotes: 4

Views: 17206

Answers (5)

kanwarprogrammer
kanwarprogrammer

Reputation: 53

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()

Upvotes: 0

Cryden
Cryden

Reputation: 75

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

Upvotes: 7

MichaelDL
MichaelDL

Reputation: 65

using tkinter 8.6U

entry.config(padx = 15)

Upvotes: 0

Kevin
Kevin

Reputation: 76194

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

Upvotes: 17

Mike - SMT
Mike - SMT

Reputation: 15226

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()

Upvotes: 0

Related Questions