Search code examples
pythonpython-3.xtkintertkinter-entry

Why does "ttk.style()" adds additional styles to the Entry widget?


So, I have 4 Entry Widgets on my window and I just wanted to add some internal left padding on the last Entry Widget. I did so using ttk.style(), which added the desired padding but it also added some additional styling like a black border, some hover effect, then the entry widget gets a blue border when selected.
This is my Code:

from tkinter import *
from tkinter import ttk

root = Tk()
root.configure(padx=50)

input1 = Entry(root)
input1.grid(row=1, column=0, pady=10)
input2 = Entry(root)
input2.grid(row=2, column=0, pady=10)
input3 = Entry(root)
input3.grid(row=3, column=0, pady=10)

style = ttk.Style(root)
style.configure('padded.TEntry', padding=[15, 0, 0, 0])

e = ttk.Entry(root, style='padded.TEntry')
e.grid(row=4,column=0, pady=10)

root.mainloop()

Look how the 4th Entry Widget has a Black Border around it

Look how the 4th Entry Widget has a Black Border around it

Look how a Blue Border appears when the widget is selected

enter image description here

The only styling that I was excepting is the little increase in width because of the left-padding, but how are these other styles being triggered.


Solution

  • It is because the fourth entry is a ttk.Entry widget but the other three are tkinter.Entry widgets. If you make all four ttk.Entry widgets you'll see that they all have the additional styles.

    Even though the tkinter and ttk modules have widgets with the same name, they are completely different widgets with their own sets of defaults.