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()
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()
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()
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()