2
from tkinter import *

from tkinter.ttk import *

from tkinter import ttk

root = Tk()

stl = ttk.Style()

stl.map('C.TButton',
     foreground = [('pressed','red'),('active','blue')],
     background = [('pressed','!disabled','black'),('active','white')]
)
#background not changing.It is still grey

ttk.Button(root, text='This is a button', style='C.TButton').pack()

root.mainloop()

I tried Using style class and made some changes in C.TButton, but it seems that it is just changing the border color instead of changing the colour of button. The Button remains grey and flat help!

MegaLegend
  • 54
  • 7
  • Does this answer your question? [how-do-i-change-the-colour-of-my-button-and-label-on-tkinter](https://stackoverflow.com/questions/44415744), [how-to-change-foreground-color-of-a-ttk-button-that-is-disabled](https://stackoverflow.com/questions/53104438) – stovfl May 17 '20 at 11:50
  • 1
    This is basically because your using a theme that cannot be edited add a line `stl.theme_use('clam')` and it might fix it or use `'default'` as theme – Delrius Euphoria Nov 24 '20 at 09:39

1 Answers1

1

I also suffered from the same problem. You can change the intended background color by using TLabel instead of TButton. However, there is no padding space around the button level text. You need to specify padding with the configure method. If you specify TLabel, Style as a button seems to be lost, and you also need to specify relief.

from tkinter import *

from tkinter.ttk import *

from tkinter import ttk

root = Tk()

stl = ttk.Style()
root.geometry('800x600')

stl = ttk.Style()
stl.configure('C.TLabel',padding=[30,10,50,60])

stl.map('C.TLabel',
    foreground = [('pressed','red'),('active','blue')],
    background = [('pressed','!disabled','black'),('active','white')],
    relief=[('pressed', 'sunken'),
            ('!pressed', 'raised')]
)

ttk.Button(root, text='This is a button', style='C.TLabel').pack()

root.mainloop()