Search code examples
pythonbuttontkintertkinter-layout

How to add hover feature for text description on a Tkinter button?


I want to add a hover feature on a Tkinter button where if the user hovers the mouse cursor then description text displays. I also want to add some delay for that description to appear so that it would not be intrusive.

I can try using the "<Enter>" and "<Leave>" binding of the button to a function and make some "Label" appear in some corner of the app. But this approach may not be the most elegant.


Solution

  • Here is a small snippet using Pmw (python mega widgets) for the tool tips.

    Firstly start by installing it:

    pip install Pmw
    

    Then here is a snippet to understand what Pmw can do:

    from tkinter import *
    import Pmw
    
    root = Tk()
    
    Pmw.initialise(root) #initializing it in the root window
    
    l = Label(root,text='Random Text')
    l.pack()
    
    b = Button(root,text='Hover me')
    b.pack()
    
    tooltip_1 = Pmw.Balloon(root) #Calling the tooltip
    tooltip_1.bind(b,'This is the hover Text\nHope you get an idea of whats going on here.') #binding it and assigning a text to it
    
    root.mainloop()
    

    Hope this gives you a better idea. Keep in mind that Pmw could create a mess while converting the py to an exe later(if you have any intentions to). There is a way around in tho.

    Cheers