Updated for 2.1b2 distribution.
[python/dscho.git] / Lib / idlelib / CallTipWindow.py
blobcbeab8cc15fc0e0bc4c6bf658f5e2719f47c795b
1 # A CallTip window class for Tkinter/IDLE.
2 # After ToolTip.py, which uses ideas gleaned from PySol
4 # Used by the CallTips IDLE extension.
5 import os
6 from Tkinter import *
8 class CallTip:
10 def __init__(self, widget):
11 self.widget = widget
12 self.tipwindow = None
13 self.id = None
14 self.x = self.y = 0
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()
32 def hidetip(self):
33 tw = self.tipwindow
34 self.tipwindow = None
35 if tw:
36 tw.destroy()
39 ###############################
41 # Test Code
43 class 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)
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)
57 text.focus_set()
58 # root.mainloop() # not in idle
60 def calltip_show(self, event):
61 self.calltip.showtip("Hello world")
63 def calltip_hide(self, event):
64 self.calltip.hidetip()
66 def main():
67 # Test code
68 c=container()
70 if __name__=='__main__':
71 main()