krazynhazy krazynhazy - 1 month ago 12
Python Question

How do I remove the Python3 tkinter ttk.CheckButton dashed line that appears after selecting the checkbox?

Is there any way to remove the dashed outline that appears when clicking the text of the ttk.Checkbutton object while using the 'default' theme?

example.)

from tkinter import *
from tkinter import ttk
master = Tk()
style = ttk.Style() # print(self.style.theme_names())
style.theme_use('default')
var = IntVar()
c = ttk.Checkbutton(master, text="Click My Text", variable=var)
c.pack()
mainloop()


If not is there any way of changing the the background button color of the standard theme used while running on windows? (Unfortunately for this project I cannot consider using any other GUI packages)

OS: Windows 10
Python: 3.5.1
Frustration level: one bajillion

Answer

Maybe I am wrong but, there doesn't seem to be a "dash off" option. However, there are still some options.

You could set the 'takefocus' option to False. This will stop the dashed outline showing, but would also remove the ability to tab between controls.

c = ttk.Checkbutton(master, text="Click My Text", variable=var, takefocus=False)

Another option would be to change the color of the dash to match the background:

style.configure('TCheckbutton', focuscolor=style.configure(".")["background"])

This kind of works, but you will need to play with the highlight color too so it's always invisible.

EDIT

While knocking up a small example to help with one of the comments, I found a slightly easier way to do this. Using cget() we can get the value of a property. This can be used instead of fiddling with the style and is easier to read and understand.

Using cget the above example becomes:

style.configure('TCheckbutton', focuscolor=master.cget("background"))

This simply sets the focus colour equal to the background colour of the master object (The window).

As requested, here is a full example based on the original code from the question:

from tkinter import *
from tkinter import ttk
master = Tk()
style = ttk.Style()
style.theme_use('default')

style.configure('TCheckbutton', focuscolor=master.cget("background"))
style.configure('TCheckbutton', activebackground=master.cget("background"))
style.configure('TCheckbutton', background=master.cget("background"))

var = IntVar()
c = ttk.Checkbutton(master, text="Click My Text", variable=var)
c.pack()
mainloop()

Here we set the focuscolor, activebackground and background colors equal to the background of the main window (master). This has the effect of removing the dashed border.

However, the dashed border is there for a reason. It shows the user which control has focus. When there is only one control on the window it looks a little strange. But when you have several it allows users to navigate through your app using the keyboard easier than it does without.