Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter Hovering over Button -> Color change

Is there a possibility to change the background-color of a Button after hovering on it? What is the code for this in Tkinter?

like image 916
Stavrius Mtvs Avatar asked Apr 17 '18 22:04

Stavrius Mtvs


People also ask

How do you change a button Colour when it is clicked Tkinter?

We can also change the foreground color by using the 'fg' property of the button widget in Tkinter. We can also use the configure method to change the color. As we know that we can use the 'bg' and 'fg' property of the Button widget to change the color, we need to pass this variable while using the Button object.

How do I change the background color of a clicking button in Python?

Python Tkinter Button Background Color You can change the background color of a Tkinter Button by setting the bg property of Tkinter Button with a color string or HEX value. Assign any standard color or rgb hex value to the bg property as shown below. The default color of Tkinter Button is grey.

What is Tk () in Tkinter?

The tkinter package (“Tk interface”) is the standard Python interface to the Tcl/Tk GUI toolkit. Both Tk and tkinter are available on most Unix platforms, including macOS, as well as on Windows systems.

How do you use Customtkinter?

To use CustomTkinter, just place the /customtkinter folder from this repository next to your program, and then you can do import customtkinter .


1 Answers

Sadly the activebackground and activeforeground options only seem to work when you are clicking on the button rather than when you hover over the button. Use the <Leave> and <Enter> events instead

import tkinter as tk

def on_enter(e):
    myButton['background'] = 'green'

def on_leave(e):
    myButton['background'] = 'SystemButtonFace'

root = tk.Tk()
myButton = tk.Button(root,text="Click Me")
myButton.grid()


myButton.bind("<Enter>", on_enter)
myButton.bind("<Leave>", on_leave)

root.mainloop()

As pointed out in the comments, if we want multiple buttons, we can bind the buttons to functions that use the event data for the click event to change the background of the button.

import tkinter as tk

def on_enter(e):
    e.widget['background'] = 'green'

def on_leave(e):
    e.widget['background'] = 'SystemButtonFace'

root = tk.Tk()
myButton = tk.Button(root,text="Click Me")
myButton.grid()


myButton.bind("<Enter>", on_enter)
myButton.bind("<Leave>", on_leave)

myButton2 = tk.Button(root,text="Click Me")
myButton2.grid()


myButton2.bind("<Enter>", on_enter)
myButton2.bind("<Leave>", on_leave)

root.mainloop()

A slicker way to do it for multiple buttons would be to create a new Button class that modifies the behaviour of the default button so that the activebackground actually works when you hover.

import tkinter as tk

class HoverButton(tk.Button):
    def __init__(self, master, **kw):
        tk.Button.__init__(self,master=master,**kw)
        self.defaultBackground = self["background"]
        self.bind("<Enter>", self.on_enter)
        self.bind("<Leave>", self.on_leave)

    def on_enter(self, e):
        self['background'] = self['activebackground']

    def on_leave(self, e):
        self['background'] = self.defaultBackground

root = tk.Tk()

classButton = HoverButton(root,text="Classy Button", activebackground='green')
classButton.grid()

root.mainloop()
like image 109
scotty3785 Avatar answered Sep 18 '22 16:09

scotty3785