Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display tooltips in Tkinter?

Tooltips are those little bits of text that popup when the mouse hovers over a widget for a certain duration of time.

How can I add a tooltip message to my tkinter Python application?

Example of tooltip

like image 802
rectangletangle Avatar asked Jul 11 '10 05:07

rectangletangle


People also ask

How do you make a tooltip in Python?

You can add a tooltip by calling . setToolTip("text") on a widget. This is often used to assist the user.

How do you display something in Tkinter?

To display data we have to use insert() function. insert function takes two parameters. In our program, we have used entry widgets to capture the information provided by the user, and then we have to frame a sentence & that sentence is displayed in another Entry widget.

What does get () do in Tkinter?

An Entry widget in Tkinter is nothing but an input widget that accepts single-line user input in a text field. To return the data entered in an Entry widget, we have to use the get() method. It returns the data of the entry widget which further can be printed on the console.


2 Answers

I tried the code in the blog post mentioned by ars, and also tried the code from the IDLE lib.

While both worked, I didn't like how the tooltip from IDLE was limited in size (had to manually enter new lines as separate lists) , and how the tips appeared immediately in the code form the blog post.

So I made a hybrid between the two. It lets you specify a wrap length and hover time, with no restriction on each:

""" 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 www.daniweb.com/programming/software-development/code/484591/a-tooltip-class-for-tkinter  Modified to include a delay time by Victor Zaccardo, 25mar16 """  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.waittime = 500     #miliseconds         self.wraplength = 180   #pixels         self.widget = widget         self.text = text         self.widget.bind("<Enter>", self.enter)         self.widget.bind("<Leave>", self.leave)         self.widget.bind("<ButtonPress>", self.leave)         self.id = None         self.tw = None      def enter(self, event=None):         self.schedule()      def leave(self, event=None):         self.unschedule()         self.hidetip()      def schedule(self):         self.unschedule()         self.id = self.widget.after(self.waittime, self.showtip)      def unschedule(self):         id = self.id         self.id = None         if id:             self.widget.after_cancel(id)      def showtip(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="#ffffff", relief='solid', borderwidth=1,                        wraplength = self.wraplength)         label.pack(ipadx=1)      def hidetip(self):         tw = self.tw         self.tw= None         if tw:             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, \    'Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, '    'consectetur, adipisci velit. Neque porro quisquam est qui dolorem ipsum '    'quia dolor sit amet, consectetur, adipisci velit. Neque porro quisquam '    'est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit.')      btn2 = tk.Button(root, text="button 2")     btn2.pack(padx=10, pady=5)     button2_ttp = CreateToolTip(btn2, \     "First thing's first, I'm the realest. Drop this and let the whole world "     "feel it. And I'm still in the Murda Bizness. I could hold you down, like "     "I'm givin' lessons in  physics. You should want a bad Vic like this.")     root.mainloop() 

Screenshot:

Example of hovertext

like image 178
crxguy52 Avatar answered Sep 23 '22 17:09

crxguy52


The Pmw.Balloon class from the Pmw toolkit for Tkinter will draw tool tips.

Also take a look at this blog post, which adapts some code from IDLE used for displaying tool tips with Tkinter.

like image 39
ars Avatar answered Sep 21 '22 17:09

ars