简体   繁体   中英

How do I change the color of a button in Python's Tkinter

I was writing a gui and I wanted to change the color of a button but background seems to change the color of an outline instead of the full background. How do I change the color of the background of a button?

I have tried the background and style.

salmon = "#FFC6AB"
black = "#0C120C"

# ADDING BUTTONS
# Adding style
style = ttk.Style()
style.configure("X.TFrame", background=grey)
style. configure("X.TButton", background=salmon, foreground=black, font=("Courier", 20), width=17)

# Adding spacer
spacer_1 = ttk.Frame(root)
spacer_1.grid(column=0, row=1, pady=12)
spacer_1.configure(style="X.TFrame")

# Adding frame
frame_2 = ttk.Frame(root)
frame_2.grid(column=0, row=2)
frame_2.configure(style="X.TFrame")

# Adding button 1
button_1 = ttk.Button(frame_2, text="Scale Cookbook")
button_1.grid(column=0, row=0)
button_1.configure(style="X.TButton")

You are doing it the right way, your code does change the button color in Linux. You don't get the result you want because the default ttk themes for Windows and Mac do not allow to change the button background color (I think because they are created from images).

If you want to change the button background color, you can change theme for one that allows it, like 'clam' or 'alt':

style = ttk.Style()
style.theme_use('clam')
# ... the rest of your style configuration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM