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()
O JOE
  • 587
  • 2
  • 17
  • 31
  • Possible duplicate of [Display message when hovering over something with mouse cursor in Python](https://stackoverflow.com/questions/20399243/display-message-when-hovering-over-something-with-mouse-cursor-in-python) - well similar enough. – Legorooj Jul 24 '19 at 16:40

3 Answers3

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()
Richard Z
  • 161
  • 2
  • 3
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.

Sazzy
  • 1,924
  • 3
  • 19
  • 27
  • This doesn't solve it, background colour should change but not border colour – O JOE Jul 24 '19 at 16:45
  • 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. – Sazzy Jul 24 '19 at 17:09
  • Seems like `ttk.Button` does not support changing background color. If you can use `tk.Button`, then you can bind events `` and `` to change its foreground and background colors. – acw1668 Jul 25 '19 at 07:49
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