Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Example
#Import required libraries
from tkinter import *
#Create an instance of tkinter frame
win= Tk()
#Define the geometry of the window
win.geometry("750x250")
#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)
win.mainloop()
Output
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.
Advertisements