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.

Edited 2 Years Ago by vegaseat

''' 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()

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!

Edited 1 Year Ago by theduke540: forgot to mention i signed up just to show my thanks

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()

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 ?

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.