00001
00002
00003 '''Michael Lange <klappnase at 8ung dot at>
00004 The ToolTip class provides a flexible tooltip widget for Tkinter; it is based on IDLE's ToolTip
00005 module which unfortunately seems to be broken (at least the version I saw).
00006 INITIALIZATION OPTIONS:
00007 anchor : where the text should be positioned inside the widget, must be on of "n", "s", "e", "w", "nw" and so on;
00008 default is "center"
00009 bd : borderwidth of the widget; default is 1 (NOTE: don't use "borderwidth" here)
00010 bg : background color to use for the widget; default is "lightyellow" (NOTE: don't use "background")
00011 delay : time in ms that it takes for the widget to appear on the screen when the mouse pointer has
00012 entered the parent widget; default is 1500
00013 fg : foreground (i.e. text) color to use; default is "black" (NOTE: don't use "foreground")
00014 follow_mouse : if set to 1 the tooltip will follow the mouse pointer instead of being displayed
00015 outside of the parent widget; this may be useful if you want to use tooltips for
00016 large widgets like listboxes or canvases; default is 0
00017 font : font to use for the widget; default is system specific
00018 justify : how multiple lines of text will be aligned, must be "left", "right" or "center"; default is "left"
00019 padx : extra space added to the left and right within the widget; default is 4
00020 pady : extra space above and below the text; default is 2
00021 relief : one of "flat", "ridge", "groove", "raised", "sunken" or "solid"; default is "solid"
00022 state : must be "normal" or "disabled"; if set to "disabled" the tooltip will not appear; default is "normal"
00023 text : the text that is displayed inside the widget
00024 textvariable : if set to an instance of Tkinter.StringVar() the variable's value will be used as text for the widget
00025 width : width of the widget; the default is 0, which means that "wraplength" will be used to limit the widgets width
00026 wraplength : limits the number of characters in each line; default is 150
00027
00028 WIDGET METHODS:
00029 configure(**opts) : change one or more of the widget's options as described above; the changes will take effect the
00030 next time the tooltip shows up; NOTE: follow_mouse cannot be changed after widget initialization
00031
00032 Other widget methods that might be useful if you want to subclass ToolTip:
00033 enter() : callback when the mouse pointer enters the parent widget
00034 leave() : called when the mouse pointer leaves the parent widget
00035 motion() : is called when the mouse pointer moves inside the parent widget if follow_mouse is set to 1 and the
00036 tooltip has shown up to continually update the coordinates of the tooltip window
00037 coords() : calculates the screen coordinates of the tooltip window
00038 create_contents() : creates the contents of the tooltip window (by default a Tkinter.Label)
00039
00040 source: http://tkinter.unpythonic.net/wiki/ToolTip
00041 '''
00042
00043
00044 import Tkinter
00045
00046 class ToolTip:
00047 def __init__(self, master, text='Your text here', delay=1500, **opts):
00048 self.master = master
00049 self._opts = {'anchor':'center', 'bd':1, 'bg':'lightyellow', 'delay':delay, 'fg':'black',\
00050 'follow_mouse':0, 'font':None, 'justify':'left', 'padx':4, 'pady':2,\
00051 'relief':'solid', 'state':'normal', 'text':text, 'textvariable':None,\
00052 'width':0, 'wraplength':150}
00053 self.configure(**opts)
00054 self._tipwindow = None
00055 self._id = None
00056 self._id1 = self.master.bind("<Enter>", self.enter, '+')
00057 self._id2 = self.master.bind("<Leave>", self.leave, '+')
00058 self._id3 = self.master.bind("<ButtonPress>", self.leave, '+')
00059 self._follow_mouse = 0
00060 if self._opts['follow_mouse']:
00061 self._id4 = self.master.bind("<Motion>", self.motion, '+')
00062 self._follow_mouse = 1
00063
00064 def configure(self, **opts):
00065 for key in opts:
00066 if self._opts.has_key(key):
00067 self._opts[key] = opts[key]
00068 else:
00069 KeyError = 'KeyError: Unknown option: "%s"' %key
00070 raise KeyError
00071
00072
00073
00074
00075 def enter(self, event=None):
00076 self._schedule()
00077
00078 def leave(self, event=None):
00079 self._unschedule()
00080 self._hide()
00081
00082 def motion(self, event=None):
00083 if self._tipwindow and self._follow_mouse:
00084 x, y = self.coords()
00085 self._tipwindow.wm_geometry("+%d+%d" % (x, y))
00086
00087
00088
00089 def _schedule(self):
00090 self._unschedule()
00091 if self._opts['state'] == 'disabled':
00092 return
00093 self._id = self.master.after(self._opts['delay'], self._show)
00094
00095 def _unschedule(self):
00096 id = self._id
00097 self._id = None
00098 if id:
00099 self.master.after_cancel(id)
00100
00101 def _show(self):
00102 if self._opts['state'] == 'disabled':
00103 self._unschedule()
00104 return
00105 if not self._tipwindow:
00106 self._tipwindow = tw = Tkinter.Toplevel(self.master)
00107
00108 tw.withdraw()
00109 tw.wm_overrideredirect(1)
00110 self.create_contents()
00111 tw.update_idletasks()
00112 x, y = self.coords()
00113 tw.wm_geometry("+%d+%d" % (x, y))
00114 tw.deiconify()
00115
00116 def _hide(self):
00117 tw = self._tipwindow
00118 self._tipwindow = None
00119 if tw:
00120 tw.destroy()
00121
00122
00123
00124 def coords(self):
00125
00126
00127
00128
00129
00130 tw = self._tipwindow
00131 twx, twy = tw.winfo_reqwidth(), tw.winfo_reqheight()
00132 w, h = tw.winfo_screenwidth(), tw.winfo_screenheight()
00133
00134 if self._follow_mouse:
00135 y = tw.winfo_pointery() + 20
00136
00137 if y + twy > h:
00138 y = y - twy - 30
00139 else:
00140 y = self.master.winfo_rooty() + self.master.winfo_height() + 3
00141 if y + twy > h:
00142 y = self.master.winfo_rooty() - twy - 3
00143
00144 x = tw.winfo_pointerx() - twx / 2
00145 if x < 0:
00146 x = 0
00147 elif x + twx > w:
00148 x = w - twx
00149 return x, y
00150
00151 def create_contents(self):
00152 opts = self._opts.copy()
00153 for opt in ('delay', 'follow_mouse', 'state'):
00154 del opts[opt]
00155 label = Tkinter.Label(self._tipwindow, **opts)
00156 label.pack()
00157
00158
00159
00160 def demo():
00161 root = Tkinter.Tk(className='ToolTip-demo')
00162 l = Tkinter.Listbox(root)
00163 l.insert('end', "I'm a listbox")
00164 l.pack(side='top')
00165 t1 = ToolTip(l, follow_mouse=1, text="I'm a tooltip with follow_mouse set to 1, so I won't be placed outside my parent")
00166 b = Tkinter.Button(root, text='Quit', command=root.quit)
00167 b.pack(side='bottom')
00168 t2 = ToolTip(b, text='Enough of this')
00169 root.mainloop()
00170
00171 if __name__ == '__main__':
00172 demo()