- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- How to change a button background color using Swift?
- How to reset the background color of a Python Tkinter button?
- How to change the background color of a Treeview in Tkinter?
- How to change the color of ttk button in Tkinter?
- How to change the background color of a tkinter Canvas dynamically?
- Change the background color of a button with CSS
- Dynamically change the widget background color in Tkinter
- How to change the menu background color of Tkinter's OptionMenu widget?
- Change the color upon hovering over Button in Tkinter
- Add hover color to background color of the table row in Bootstrap
- How to set the background color of a ttk.Combobox in tkinter?
- How to change the background color using jQuery?
- How to change JFrame background color in Java
- How to change axes background color in Matplotlib?
- How to make a Button using the Tkinter Canvas widget?

Advertisements