2

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

petro4213
  • 163
  • 9
  • Why do you want to hide it? Is serves a specific purpose by letting users know where the keyboard focus is. – Bryan Oakley Mar 10 '22 at 20:54
  • Hi Bryan, I want to hide it, because it's ugly and my users usually don't operate the widgets with the space bar - they just klick them with the mouse, so the focus rectangle makes no sense and just distracts. – petro4213 Mar 11 '22 at 16:49

1 Answers1

2

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.

Sylvester Kruin
  • 3,294
  • 5
  • 16
  • 39