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
Selected Reading
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.
Example
# 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.

Advertisements
