blob: d253fa5969a9cb3a5161f6b67bb05ac5bc3ca3ef [file] [log] [blame]
David Scherer7aced172000-08-15 01:13:23 +00001# A CallTip window class for Tkinter/IDLE.
2# After ToolTip.py, which uses ideas gleaned from PySol
3
4# Used by the CallTips IDLE extension.
5import os
6from Tkinter import *
7
8class CallTip:
9
10 def __init__(self, widget):
11 self.widget = widget
12 self.tipwindow = None
13 self.id = None
14 self.x = self.y = 0
15
16 def showtip(self, text):
17 self.text = text
18 if self.tipwindow or not self.text:
19 return
20 self.widget.see("insert")
21 x, y, cx, cy = self.widget.bbox("insert")
22 x = x + self.widget.winfo_rootx() + 2
23 y = y + cy + self.widget.winfo_rooty()
24 self.tipwindow = tw = Toplevel(self.widget)
25 tw.wm_overrideredirect(1)
26 tw.wm_geometry("+%d+%d" % (x, y))
27 label = Label(tw, text=self.text, justify=LEFT,
28 background="#ffffe0", relief=SOLID, borderwidth=1,
29 font = self.widget['font'])
30 label.pack()
Kurt B. Kaiser9a1ae1a2001-07-12 22:26:44 +000031
David Scherer7aced172000-08-15 01:13:23 +000032 def hidetip(self):
33 tw = self.tipwindow
34 self.tipwindow = None
35 if tw:
36 tw.destroy()
37
38
39###############################
40#
41# Test Code
42#
43class container: # Conceptually an editor_window
44 def __init__(self):
45 root = Tk()
46 text = self.text = Text(root)
47 text.pack(side=LEFT, fill=BOTH, expand=1)
48 text.insert("insert", "string.split")
49 root.update()
50 self.calltip = CallTip(text)
51
52 text.event_add("<<calltip-show>>", "(")
53 text.event_add("<<calltip-hide>>", ")")
54 text.bind("<<calltip-show>>", self.calltip_show)
55 text.bind("<<calltip-hide>>", self.calltip_hide)
Kurt B. Kaiser9a1ae1a2001-07-12 22:26:44 +000056
David Scherer7aced172000-08-15 01:13:23 +000057 text.focus_set()
58 # root.mainloop() # not in idle
59
60 def calltip_show(self, event):
61 self.calltip.showtip("Hello world")
62
63 def calltip_hide(self, event):
64 self.calltip.hidetip()
65
66def main():
67 # Test code
68 c=container()
69
70if __name__=='__main__':
71 main()