Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to make a Button Hover to change the Background Color in Tkinter?
A Button widget in Tkinter has many built-in features which can be used to configure and perform certain tasks 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.
Example
Here's how to create a button that changes the background color when hovering ?
# Import the required libraries
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame or window
win = Tk()
# Set the size of the window
win.geometry("700x350")
def change_bgcolor(e):
win.config(background="green3")
def change_fgcolor(e):
win.config(background="white")
# Add Buttons to trigger the event
b1 = Button(win, text="Hover on Me", font=('Georgia', 16))
b1.pack(pady=60, anchor=CENTER)
# Bind the events
for b in [b1]:
b.bind("<Enter>", change_bgcolor)
b.bind("<Leave>", change_fgcolor)
win.mainloop()
Output
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.

How It Works
The <Enter> event is triggered when the mouse cursor enters the button widget area, and <Leave> is triggered when it leaves. The bind method connects these events to callback functions that change the window's background color.
Button-Specific Hover Effect
You can also change the button's own appearance instead of the window background ?
from tkinter import *
win = Tk()
win.geometry("400x300")
def on_enter(e):
button['background'] = 'lightblue'
button['foreground'] = 'red'
def on_leave(e):
button['background'] = 'SystemButtonFace'
button['foreground'] = 'black'
button = Button(win, text="Hover Effect", font=('Arial', 14))
button.pack(pady=50)
button.bind("<Enter>", on_enter)
button.bind("<Leave>", on_leave)
win.mainloop()
Conclusion
Use the <Enter> and <Leave> events with the bind method to create hover effects on Tkinter buttons. You can modify either the button's appearance or the parent window's properties to provide visual feedback to users.
