2

I am trying to change the focus color of a ttk.Entry widget in python (blue border):

enter image description here

I know that we can change the focus color of the notebook tabs with style.configure('Tab', focuscolor='red'), so I wonder how to do it with an entry widget?

Here is my code:

import tkinter
from tkinter import ttk

root = tkinter.Tk()

style = ttk.Style()
style.theme_use('clam')
style.configure('TEntry', focuscolor='red') # don't work

ttk.Entry(root).grid(padx=10, pady=10)

root.mainloop()
Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
Marin Nagy
  • 297
  • 1
  • 10
  • if you don't want that , then don't apply any theme – eshirvana Mar 14 '22 at 18:35
  • Same problem with the default macOS theme, and I really want to use a special theme... – Marin Nagy Mar 14 '22 at 18:38
  • 1
    "focuscolor" isn't an option. Try `bordercolor` or `lightcolor` in addition, it is possible you need to use style.map to avoid your color gets changed by default. See my answer [here](https://stackoverflow.com/a/65464537/13629335) for a list of opinions available. You may want to take a look on my answer [here](https://stackoverflow.com/a/69890743/13629335) for a brief description of ttk.style, because it may appear that the options you need aren't a choice in the current theme or your system. Also @eshirvana note that there is always a theme set by default. – Thingamabobs Mar 14 '22 at 18:53
  • @Thingamabobs I already tried the two options, both change the border color of the widget but the focus is still here. I looked at you answers, these are very complet and so you think this is not possible to change my focus color? PS: I took the `clam` theme for the exemple but in reality I use the `awdark` theme from [awthemes](https://wiki.tcl-lang.org/page/awthemes) – Marin Nagy Mar 14 '22 at 19:40
  • @MarinNagy these answers are far from complete, I'd tried to cover the important content. See this [tutorial](https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/ttk-map.html) and try to map the `highlightcolor`. Also note that sometimes features of tk aren't available in ttk. You may end up using a tk.Entry and configure there a highlightcolor. – Thingamabobs Mar 14 '22 at 20:09
  • 2
    Try `style.map('TEntry', lightcolor=[('focus', 'red')])`. – acw1668 Mar 15 '22 at 04:04
  • It works, I didn't know that we can map the `focus` event of a widget for styling. Thank you! – Marin Nagy Mar 15 '22 at 17:00

1 Answers1

2

As suggested by @Thingamabobs and @acw1668 It's possible to change the focus color of a ttk.Entry by mapping the focus state in its style property. Here is the working code:

import tkinter
from tkinter import ttk

root = tkinter.Tk()

style = ttk.Style()
style.theme_use('clam')
style.map('TEntry', lightcolor=[('focus', 'white')])

ttk.Entry(root).grid(padx=10, pady=10)

root.mainloop()

My final goal was to hide the focus border so I changed its color to white (the background color), and now the result looks like this: (The black border is just the original border of the entry)

ttk entry without focus border

Marin Nagy
  • 297
  • 1
  • 10