Skip to content

Instantly share code, notes, and snippets.

@vigilantPotato
Last active April 24, 2023 13:57
Show Gist options
  • Save vigilantPotato/9541919d4b8cd04c6dd6e253a5cb5daf to your computer and use it in GitHub Desktop.
Save vigilantPotato/9541919d4b8cd04c6dd6e253a5cb5daf to your computer and use it in GitHub Desktop.
how to change tkinter's widget color on hover
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()
@vigilantPotato
Copy link
Author

mouse leave
leave

on hover
on_hover

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment