Is there a consistent way to remove the focus rectangle from ttk widgets

Viewed 127

I have this minimal tkinter program:

import tkinter
import tkinter.ttk as ttk

root = tkinter.Tk()

s = ttk.Style()
s.theme_use("winnative")

b1 = ttk.Button(root, text="Button")
b1.pack()
b2 = ttk.Checkbutton(root, text="Checkbutton")
b2.pack()
v = tkinter.IntVar()
v.set(0)
b3 = ttk.Radiobutton(root, text="option 1", variable=v, value=0)
b4 = ttk.Radiobutton(root, text="option 2", variable=v, value=1)
b3.pack()
b4.pack()

tkinter.mainloop()

When I run it, I get this GUI (see image below). When I click one of the widgets, it gets a focus rectangle around (the dashed rectangle). I would like to suppress this but I can't find any documentation about it.
Is there a consistent way of removing this rectangle for all the widget types. I found one solution for the Notebook tabs (here Removing Ttk Notebook Tab Dashed Line), but I can't figure out how to apply it to other widgets.

enter image description here

1 Answers

If you don't want to have the widgets focus, than the simplest solution is to make it so that they can't take focus. For this, you can use takefocus=False. Here is what your code should look like:

import tkinter
import tkinter.ttk as ttk

root = tkinter.Tk()

s = ttk.Style()
s.theme_use("winnative")

b1 = ttk.Button(root, text="Button", takefocus=False)
b1.pack()
b2 = ttk.Checkbutton(root, text="Checkbutton", takefocus=False)
b2.pack()
v = tkinter.IntVar()
v.set(0)
b3 = ttk.Radiobutton(root, text="option 1", variable=v, value=0, takefocus=False)
b4 = ttk.Radiobutton(root, text="option 2", variable=v, value=1, takefocus=False)
b3.pack()
b4.pack()

tkinter.mainloop()

Now, the widgets can't take focus (which, judging by your comment, should be fine), and they don't have that ugly border anymore.

Related