A tooltip class for Tkinter

vegaseat 7 Tallied Votes 13K Views Share

This class gives a specified Tkinter widget a tooltip that appears as the mouse is above the widget. You can improve the code by putting in a time delay.

''' tk_ToolTip_class101.py
gives a Tkinter widget a tooltip as the mouse is above the widget
tested with Python27 and Python34  by  vegaseat  09sep2014
'''

try:
    # for Python2
    import Tkinter as tk
except ImportError:
    # for Python3
    import tkinter as tk


class CreateToolTip(object):
    '''
    create a tooltip for a given widget
    '''
    def __init__(self, widget, text='widget info'):
        self.widget = widget
        self.text = text
        self.widget.bind("<Enter>", self.enter)
        self.widget.bind("<Leave>", self.close)

    def enter(self, event=None):
        x = y = 0
        x, y, cx, cy = self.widget.bbox("insert")
        x += self.widget.winfo_rootx() + 25
        y += self.widget.winfo_rooty() + 20
        # creates a toplevel window
        self.tw = tk.Toplevel(self.widget)
        # Leaves only the label and removes the app window
        self.tw.wm_overrideredirect(True)
        self.tw.wm_geometry("+%d+%d" % (x, y))
        label = tk.Label(self.tw, text=self.text, justify='left',
                       background='yellow', relief='solid', borderwidth=1,
                       font=("times", "8", "normal"))
        label.pack(ipadx=1)

    def close(self, event=None):
        if self.tw:
            self.tw.destroy()

# testing ...
if __name__ == '__main__':
    root = tk.Tk()

    btn1 = tk.Button(root, text="button 1")
    btn1.pack(padx=10, pady=5)
    button1_ttp = CreateToolTip(btn1, "mouse is over button 1")

    btn2 = tk.Button(root, text="button 2")
    btn2.pack(padx=10, pady=5)
    button2_ttp = CreateToolTip(btn2, "mouse is over button 2")

    root.mainloop()
theduke540 0 Newbie Poster

This is excellent! Just what I was looking for. I tried Tix, but had no luck. This is simple and it works. I had to sign up just to show my thanks!

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Just to mention it ...

''' tix_Balloon102.py
Tkinter extension module tix comes with Python27 and Python3+
tix has addtional widgets like ...
tix.Balloon (acts like a tooltip)
'''

try:
    # Python2
    import Tix as tix
except ImportError:
    # Python3
    import tkinter.tix as tix

root = tix.Tk()

def hello():
    label['text'] = 'Hello World'
    #label.config(text='Hello World')

def bye():
    label['text'] = 'Bye Cruel World'

label = tix.Label(root, width=40, relief=tix.SUNKEN, bd=1)
btn1 = tix.Button(root, text="Hello", command=hello)
btn2 = tix.Button(root, text="Bye", command=bye)

# create balloon (tooltip) instance
balloon = tix.Balloon(root)

# bind balloon to buttons
balloon.bind_widget(btn1, balloonmsg='Click to show Hallo')
balloon.bind_widget(btn2, balloonmsg='Click to show Bye')

# layout, stack vertically
label.pack()
btn1.pack(pady=8)
btn2.pack(pady=8)

root.mainloop()
fonzali 0 Light Poster

the code works fine on my windows machine but on ubuntu 14.04 it says it can not find ' tix ' . do I need to install it separately ?

Marc_10 0 Newbie Poster

In the 1st class you can add in line 38:
label.after(1000, self.close)
to disappear the label after 1 sec

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.