-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToolTip.py
50 lines (43 loc) · 1.75 KB
/
ToolTip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'''
May 2017
@author: Burkhard A. Meier
'''
#======================
# imports
#======================
import tkinter as tk
#===================================================================
class ToolTip(object):
def __init__(self, widget):
self.widget = widget
self.tip_window = None
def show_tip(self, tip_text):
"Display text in a tooltip window"
if self.tip_window or not tip_text:
return
x, y, _cx, cy = self.widget.bbox("insert") # get size of widget
x = x + self.widget.winfo_rootx() + 25 # calculate to display tooltip
y = y + cy + self.widget.winfo_rooty() + 25 # below and to the right
self.tip_window = tw = tk.Toplevel(self.widget) # create new tooltip window
tw.wm_overrideredirect(True) # remove all Window Manager (wm) decorations
# tw.wm_overrideredirect(False) # uncomment to see the effect
tw.wm_geometry("+%d+%d" % (x, y)) # create window size
label = tk.Label(tw, text=tip_text, justify=tk.LEFT,
background="#ffffe0", relief=tk.SOLID, borderwidth=1,
font=("tahoma", "8", "normal"))
label.pack(ipadx=1)
def hide_tip(self):
tw = self.tip_window
self.tip_window = None
if tw:
tw.destroy()
#===================================================================
def create_ToolTip(widget, text):
toolTip = ToolTip(widget) # create instance of class
def enter(event):
try: toolTip.show_tip(text)
except: pass
def leave(event):
toolTip.hide_tip()
widget.bind('<Enter>', enter) # bind mouse events
widget.bind('<Leave>', leave)