Last active
April 24, 2023 13:57
-
-
Save vigilantPotato/9541919d4b8cd04c6dd6e253a5cb5daf to your computer and use it in GitHub Desktop.
how to change tkinter's widget color on hover
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tkinter | |
class ChangeColorOnHoverButton(tkinter.Button): | |
""" | |
button class which changes its background color on hover | |
""" | |
def __init__(self, root): | |
super().__init__( | |
root, | |
text="Change color on hover", | |
bg="cyan", | |
fg="black", | |
) | |
self.bind("<Motion>", self.mouse_on) | |
self.bind("<Leave>", self.mouse_leave) | |
def mouse_on(self, event): | |
self["background"] = "cyan4" | |
self["foreground"] = "white" | |
def mouse_leave(self, event): | |
self["background"] = "cyan" | |
self["foreground"] = "black" | |
if __name__ == "__main__": | |
root = tkinter.Tk() | |
b = ChangeColorOnHoverButton(root) | |
b.pack() | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
mouse leave

on hover
