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.
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()
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()
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.
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 wrote 1 ] 1
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.