change the color of the button on hovering tkinter

Solutions on MaxInterview for change the color of the button on hovering tkinter by the best coders in the world

showing results for - "change the color of the button on hovering tkinter"
Alejandra
28 Jul 2020
1import tkinter as tk
2
3def on_enter(e):
4    myButton['background'] = 'green'
5
6def on_leave(e):
7    myButton['background'] = 'SystemButtonFace'
8
9root = tk.Tk()
10myButton = tk.Button(root,text="Click Me")
11myButton.grid()
12
13
14myButton.bind("<Enter>", on_enter)
15myButton.bind("<Leave>", on_leave)
16
17root.mainloop()
18
Evelyn
03 Feb 2016
1import tkinter as tk
2
3class HoverButton(tk.Button):
4    def __init__(self, master, **kw):
5        tk.Button.__init__(self,master=master,**kw)
6        self.defaultBackground = self["background"]
7        self.bind("<Enter>", self.on_enter)
8        self.bind("<Leave>", self.on_leave)
9
10    def on_enter(self, e):
11        self['background'] = self['activebackground']
12
13    def on_leave(self, e):
14        self['background'] = self.defaultBackground
15
16root = tk.Tk()
17
18classButton = HoverButton(root,text="Classy Button", activebackground='green')
19classButton.grid()
20
21root.mainloop()
22