How to make a Button Hover to change the Background Color in Tkinter?

A Button widget in Tkinter has many inbuilt features which can be used to configure and perform a certain task in the application. In order to run a particular event in the application, we can use the bind("<Buttons>", callback) method to bind a function or event with the button. To add the hover property in the Button, we can use <Enter> and <Leave> parameters in the bind function.


# Import the required libraries
from tkinter import *
from tkinter import ttk

# Create an instance of tkinter frame or window

# Set the size of the window

def change_bgcolor(e):

def change_fgcolor(e):

# Add Buttons to trigger the event
b1=Button(win, text="Hover on Me", font=('Georgia 16'))

# Bind the events
for b in [b1]:
   b.bind("<Leave>", change_fgcolor)



If we run the above code, it will display a window that contains a button.

When we hover the mouse on the Button, it will change the background color of the main window.