Sun Bear
Sun Bear

Reputation: 8234

How to change the colour of a ttk.Checkbutton when it is !disabled and selected?

How to change the colour of the indicator of ttk.Checkbutton when it is !disabled and selected? The following picture shows it is blueish in colour.

sample

Typing the following:

import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
s = ttk.Style()
print(f"{s.map('TCheckbutton', 'indicatorcolor')=}")

returns:

s.map('TCheckbutton', 'indicatorcolor')=[('pressed', '#ececec'), ('!disabled', 'alternate', '#9fbdd8'), ('disabled', 'alternate', '#c0c0c0'), ('!disabled', 'selected', '#4a6984'), ('disabled', 'selected', '#a3a3a3')]

I tried to change ('!disabled', 'selected', '#4a6984') to red color by using

s.map('TCheckbutton.indicatorcolor',
          background=[('pressed', '#ececec'),
                      ('!disabled', 'alternate', 'red'),
                      ('disabled', 'alternate', '#c0c0c0'),
                      # ('!disabled', 'selected', '#4a6984'),  # original
                      ('!disabled', 'selected', 'red'),
                      ('disabled', 'selected', '#a3a3a3')]
          )

and replacing the word background with foreground and even removing the word entirely but these methods failed to work. I also tried the below syntax but to no avail.

ss.configure("TCheckbutton.indicator.", background="red", foreground="red",
             indicatorcolor="red")

Upvotes: 2

Views: 602

Answers (1)

Thingamabobs
Thingamabobs

Reputation: 8037

For me under Windows 11 and the theme alt the indicatorcolor corresponds with the forground option and the box of the indicator is colored by the indicatorcolor. So it might be confusing. But I think the code below should show how it is done

import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
style = ttk.Style()
common_fg = 'white'
common_bg = 'black'
sel_bg = 'red'
style.theme_use('alt')
style.configure('TCheckbutton',
                focuscolor = '',
                font=['arial','10','italic'])

style.map('TCheckbutton',
          foreground = [('disabled', common_fg),
                      ('pressed', common_fg),
                      ('active', 'blue')],
##          background = [('disabled', common_bg),
##                      ('pressed', '!focus', common_bg),
##                      ('active', common_bg)],
          indicatorcolor=[('selected', 'red'),
                          ('pressed', 'pink')]
          )
c = ttk.Checkbutton(root, text='test')
c.pack()
root.mainloop()

Upvotes: 1

Related Questions