Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 927
Programer Beginner Avatar asked Aug 24 '26 11:08

Programer Beginner


2 Answers

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

like image 106
Kevin Avatar answered Aug 26 '26 00:08

Kevin


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

like image 41
Cryden Avatar answered Aug 25 '26 23:08

Cryden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!