-
Notifications
You must be signed in to change notification settings - Fork 1
/
ToolTip_UI.py
66 lines (56 loc) · 1.85 KB
/
ToolTip_UI.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'''
Created on 10 Apr 2016
@author: Tom Barrett
'''
import Tkinter as tk
class ToolTip(object):
def __init__(self, widget, text, openDelay=2000):
self.widget = widget
self.tipwindow = None
self.id = None
self.text = text
self.openDelay = openDelay
self.x = self.y = 0
def updateText(self, text):
self.text=text
def spawntip(self):
self.id = self.widget.after(self.openDelay, self.showtip)
def showtip(self):
'''Display text in tooltip window'''
if self.tipwindow or not self.text:
return
x, y, cx, cy = self.widget.bbox("insert")
x = x + self.widget.winfo_rootx() + 27
y = y + cy + self.widget.winfo_rooty() +27
self.tipwindow = tw = tk.Toplevel(self.widget)
tw.wm_overrideredirect(1)
tw.wm_geometry("+%d+%d" % (x, y))
try:
# For Mac OS
tw.tk.call("::tk::unsupported::MacWindowStyle",
"style", tw._w,
"help", "noActivates")
except tk.TclError:
pass
label = tk.Label(tw, text=self.text, justify=tk.LEFT,
background="#ffffe0", relief=tk.SOLID, borderwidth=1,
font=("tahoma", "8", "normal"))
label.pack(ipadx=1)
def hidetip(self):
try:
self.widget.after_cancel(self.id)
except: pass
finally:
tw = self.tipwindow
self.tipwindow = None
if tw:
tw.destroy()
def createToolTip(widget, text, openDelay=1000):
toolTip = ToolTip(widget, text, openDelay)
def enter(event):
toolTip.spawntip()
def leave(event):
toolTip.hidetip()
widget.bind('<Enter>', enter)
widget.bind('<Leave>', leave)
return toolTip