Search code examples
buttontkinterpython-3.7ttk

Is there any way to change the background color of ttk buttons in python? I tried using style method but it just changes the border colour


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!


Solution

  • 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()