5

I'm trying to change ttk.tkinter button background to black and foreground colour to white when mouse is hover it. Have tried highlightbackground and activebackground but doesn't yield the result I'm looking for.

Desired button image

import tkinter as tk
import tkinter.ttk as ttk


root = tk.Tk()

style = ttk.Style(root)
#style.theme_use("clam")

style.configure('TButton', foreground="black", highlightthickness=5,
                highlightbackground='#3E4149', highlightforeground="white",
                activebackground="black")

btr = ttk.Button(root, text="TEST BUTTON")
btr.pack()

root.mainloop()
1

3 Answers 3

9

ttk Button appearances are driven by themes (3D/Color-alt/classic/default, Color-clam). Not setting/others leaves buttons flat/grey and settings don't change things. To make a ttk TButton change colors can be achieved using map. 3D appearance requires borderwidth.Only Classic forms an outer ring using highlight. Similar answer see: Python: Changing ttk button color depending on current color?

import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
style = ttk.Style()
style.theme_use("classic")

style.map("C.TButton",
   foreground=[('!active', 'black'),('pressed', 'red'), ('active', 'white')],
    background=[ ('!active','grey75'),('pressed', 'green'), ('active', 'black')]
    )
btr = ttk.Button(root, text="TEST BUTTON", style="C.TButton")
btr.grid(column=0,row=0,sticky='nsew');
root.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

2

Try using the map function with your style, as described here:

https://docs.python.org/3/library/tkinter.ttk.html

import tkinter as tk
import tkinter.ttk as ttk


root = tk.Tk()

style = ttk.Style(root)
#style.theme_use("clam")


style.map("C.TButton",
    foreground=[('pressed', 'red'), ('active', 'blue')],
    background=[('pressed', '!disabled', 'black'), ('active', 'white')]
    )

btr = ttk.Button(root, text="TEST BUTTON", style="C.TButton")
btr.pack()

root.mainloop()

Register the style map with the button.

I hope this helps.

3 Comments

This doesn't solve it, background colour should change but not border colour
Use tk.Button instead of ttk.Button, as the tk.Button supports greater button shape customization. Options listed in your Style config do not exist for the ttk.Button variant.
Seems like ttk.Button does not support changing background color. If you can use tk.Button, then you can bind events <Enter> and <Leave> to change its foreground and background colors.
0

you have to try this I was having this problem before I learned this Code

import tkinter 
from tkinter import ttk
from tkinter import *
import tkinter.ttk


f=Tk()

style = ttk.Style()
style.configure("BW.TLabel", foreground="blue", 
background="red")

l1 = ttk.Label(f,text="Test", style="BW.TLabel")
l2 = ttk.Label(f,text="Test", style="BW.TLabel")
l1.pack()
l2.pack()
f.mainloop(

you must see this documentation in python.org website [[it will learn you a lot of things like that I wrote1]1

1 Comment

U used too many duplicates from import ttk

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.