Python Forum

Full Version: Hover event
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good morning all, I have been reading a lot of different threads on various sites for information on how to make a simple hover routine for when a mouse is over and off a button widget.
The binding is not the problem but the actual routine for the <Enter> and <Leave> is where I am a little stumped and having a problem.

Is there a simple or minimal code that can handle this type of scenario?
you need a copy of John Shipman's Tkinter reference manual
has all of the attributes and how to implement them.
Ignore date written, Tkinter has changed little since the manual was written
you can get a copy here: http://reu.cct.lsu.edu/documents/Python_...kinter.pdf
I appreciate the link Larz60. I have a copy of this already and before posting I checked for information on Hover in it, along with other PDFs and sites, but I have found very little that is informative to explain how it works or the simplest manner to achieve it.

I found a couple code examples which seems to use other libs to achieve the effect and the example code was quite large. Surely there is a simple minimal code way to have the mouse hover over a button and it give an popup to allow the user to see what the button function is.
Here's an example that I just wrote.
the module cam be imported using 'import ButtonHoverExample' but is setup to run from command line as is.
you can get colors from: https://www.w3schools.com/colors/colors_picker.asp

Name this: ButtonHoverExample.py and run from command line with
python3 ButtonHoverExample
import tkinter as tk


class ButtonHoverExample:
    def __init__(self, parent):
        self.parent = parent
        self.parent.title("HoverExample")
        self.parent.geometry("160x60+10+10")
        self.buttons = {}

    def on_enter(self, name):
        button = self.buttons[name]
        # print(f"\nMouse Enter - name: {button['name']}, details: {button}")
        bgcolor = button["mouseexit"]
        button["button"].configure(bg=bgcolor)

    def on_exit(self, name):
        button = self.buttons[name]
        bgcolor = button["mouseenter"]
        button["button"].configure(bg=bgcolor)

    def new_button(self, name, mecolor, mxcolor, xpos, ypos):
        button = self.buttons
        parent = self.parent

        button[name] = {}
        button[name]["name"] = name
        button[name]["mouseenter"] = mecolor
        button[name]["mouseexit"] = mxcolor
        button[name]["xpos"] = xpos
        button[name]["ypos"] = ypos
        button[name]["button"] = tk.Button(
            parent, text=name, bg=button[name]["mouseenter"]
        )
        button[name]["button"].grid(row=xpos, column=ypos)

        # For binding options see Shipman 54.3. Event types
        button[name]["button"].bind("<Enter>", lambda event: self.on_enter(name))
        button[name]["button"].bind("<Leave>", lambda event: self.on_exit(name))

    def show_buttons(self, name):
        for key, value in self.buttons[name].items():
            print(f"{key}: {value}")


def main():
    root = tk.Tk()
    hx = ButtonHoverExample(root)

    # get colors here: https://www.w3schools.com/colors/colors_picker.asp
    hx.new_button(
        name="Button1", mecolor="#ccccff", mxcolor="#ffcccc", xpos=10, ypos=10
    )
    hx.new_button(
        name="Button2", mecolor="#ccffff", mxcolor="#ffffcc", xpos=10, ypos=100
    )

    # to show stored values:
    hx.show_buttons("Button1")

    root.mainloop()


if __name__ == "__main__":
    main()
Thanks again Larz60+, but this will change the color of the buttons while I am trying to add an info popup when hovering over each button.
example: A button named "Print" would show a text "print selection" when the mouse is over the button and clear when is leaves the field.
so take the color out. Simple, eh?
Thanks Larz60+
Your welcome
dt200000 are looking for a tool tip with text when you hover over a widget?
the PMW module has an easy way to achieve this. There's a couple examples on github
search tkinter tooltip
Thanks joe_momma, I will look into this as well. The more information I can get and learn from the better I will be in the end. :)