blob: d253fa5969a9cb3a5161f6b67bb05ac5bc3ca3ef [file] [log] [blame]
Guido van Rossum85b97351999-06-02 16:10:19 +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,
Guido van Rossumab3b50b1999-06-10 14:19:46 +000028 background="#ffffe0", relief=SOLID, borderwidth=1,
29 font = self.widget['font'])
Guido van Rossum85b97351999-06-02 16:10:19 +000030 label.pack()
Tim Peters70c43782001-01-17 08:48:39 +000031
Guido van Rossum85b97351999-06-02 16:10:19 +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)
Tim Peters70c43782001-01-17 08:48:39 +000056
Guido van Rossum85b97351999-06-02 16:10:19 +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()