Skip to content

Instantly share code, notes, and snippets.

@vigilantPotato
Last active April 24, 2023 13:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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