Python Forum
[Tkinter] Hover event
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Hover event
#1
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?
"Often stumped... But never defeated."
Reply
#2
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
Reply
#3
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.
"Often stumped... But never defeated."
Reply
#4
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()
Reply
#5
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.
"Often stumped... But never defeated."
Reply
#6
so take the color out. Simple, eh?
Reply
#7
Thanks Larz60+
"Often stumped... But never defeated."
Reply
#8
Your welcome
Reply
#9
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
Reply
#10
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. :)
"Often stumped... But never defeated."
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] Hover over highlighted text and open popup window DrakeSoft 2 1,503 Oct-29-2022, 04:30 PM
Last Post: DrakeSoft
  [Tkinter] Glow text of clickable object on hover with transition andy 6 6,038 May-11-2021, 07:39 AM
Last Post: andy
  [PyQt] Ubuntu Taskbar icon shows title as "Unknown" while hover davidmuni 3 3,729 Jan-28-2020, 01:13 PM
Last Post: davidmuni
  Help: Enlarge image on hover morganruben 1 3,202 May-22-2018, 10:08 AM
Last Post: j.crater
  bind hover on tkinter.ttk.Treeview Larz60+ 4 15,028 May-20-2017, 10:28 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020