Can I make a Tk button's background color change only while it's being held down?

Question:

Black window with a white button

when i hold it down i want it to be another color when i hold the button without releasing it

Black window with a light grey button

from tkinter import *

root = Tk()
root.config(bg="Black")
button = Button(root, text='Ejemplo',bg='white')
button.pack()
root.mainloop()
Asked By: 死 J4K17F0RN死

||

Answers:

You can manage the color with activebackground="colorname":

Color Example:

import tkinter as tk

win= tk.Tk()
win.geometry("300x250")
win.config(bg="Black")

#Define hover functions
def on_enter(e):
    button.config(background='OrangeRed3', foreground= "white")

def on_leave(e):
    button.config(background= 'SystemButtonFace', foreground= 'black')
   
#Create color Button active and non active
button = tk.Button(win, text= "Ejemplo", font= ('Helvetica 13 bold'), fg='red', bg='white', activebackground="lightgray", activeforeground="red")
button.pack(pady= 20)

#Bind the Enter and Leave Events to the Button
button.bind('<Enter>', on_enter)
button.bind('<Leave>', on_leave)
win.mainloop()
Answered By: Hermann12
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.