Change the color upon hovering over Button in Tkinter

Let us suppose that we are creating an application in which we want to change the color of the Button widget while we hover upon it. We can have the hovering property by defining the Event Callbacks.

To change the color of the Button while hovering on it, we have to bind the <Enter> and <Leave> events. For each event, we will configure the button property such as background color, foreground color, etc.


#Import required libraries
from tkinter import *
#Create an instance of tkinter frame
win= Tk()
#Define the geometry of the window
#Define functions
def on_enter(e):
   button.config(background='OrangeRed3', foreground= "white")

def on_leave(e):
   button.config(background= 'SystemButtonFace', foreground= 'black')
#Create a Button
button= Button(win, text= "Click Me", font= ('Helvetica 13 bold'))
button.pack(pady= 20)

#Bind the Enter and Leave Events to the Button
button.bind('<Enter>', on_enter)
button.bind('<Leave>', on_leave)


Running the above code will display a window that contains a button.

Now, hover over the button in the displayed window. It will change its basic color such as background color and foreground color.

Updated on: 03-May-2021

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started