Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | """A CallTip window class for Tkinter/IDLE. |
2 | ||
3 | After ToolTip.py, which uses ideas gleaned from PySol | |
4 | Used by the CallTips IDLE extension. | |
5 | ||
6 | """ | |
7 | from Tkinter import * | |
8 | ||
9 | class CallTip: | |
10 | ||
11 | def __init__(self, widget): | |
12 | self.widget = widget | |
13 | self.tipwindow = None | |
14 | self.id = None | |
15 | self.x = self.y = 0 | |
16 | ||
17 | def showtip(self, text): | |
18 | " Display text in calltip window" | |
19 | # truncate overly long calltip | |
20 | if len(text) >= 79: | |
21 | text = text[:75] + ' ...' | |
22 | self.text = text | |
23 | if self.tipwindow or not self.text: | |
24 | return | |
25 | self.widget.see("insert") | |
26 | x, y, cx, cy = self.widget.bbox("insert") | |
27 | x = x + self.widget.winfo_rootx() + 2 | |
28 | y = y + cy + self.widget.winfo_rooty() | |
29 | self.tipwindow = tw = Toplevel(self.widget) | |
30 | # XXX 12 Dec 2002 KBK The following command has two effects: It removes | |
31 | # the calltip window border (good) but also causes (at least on | |
32 | # Linux) the calltip to show as a top level window, burning through | |
33 | # any other window dragged over it. Also, shows on all viewports! | |
34 | tw.wm_overrideredirect(1) | |
35 | tw.wm_geometry("+%d+%d" % (x, y)) | |
36 | try: | |
37 | # This command is only needed and available on Tk >= 8.4.0 for OSX | |
38 | # Without it, call tips intrude on the typing process by grabbing | |
39 | # the focus. | |
40 | tw.tk.call("::tk::unsupported::MacWindowStyle", "style", tw._w, | |
41 | "help", "noActivates") | |
42 | except TclError: | |
43 | pass | |
44 | label = Label(tw, text=self.text, justify=LEFT, | |
45 | background="#ffffe0", relief=SOLID, borderwidth=1, | |
46 | font = self.widget['font']) | |
47 | label.pack() | |
48 | ||
49 | def hidetip(self): | |
50 | tw = self.tipwindow | |
51 | self.tipwindow = None | |
52 | if tw: | |
53 | tw.destroy() | |
54 | ||
55 | ||
56 | ############################### | |
57 | # | |
58 | # Test Code | |
59 | # | |
60 | class container: # Conceptually an editor_window | |
61 | def __init__(self): | |
62 | root = Tk() | |
63 | text = self.text = Text(root) | |
64 | text.pack(side=LEFT, fill=BOTH, expand=1) | |
65 | text.insert("insert", "string.split") | |
66 | root.update() | |
67 | self.calltip = CallTip(text) | |
68 | ||
69 | text.event_add("<<calltip-show>>", "(") | |
70 | text.event_add("<<calltip-hide>>", ")") | |
71 | text.bind("<<calltip-show>>", self.calltip_show) | |
72 | text.bind("<<calltip-hide>>", self.calltip_hide) | |
73 | ||
74 | text.focus_set() | |
75 | root.mainloop() | |
76 | ||
77 | def calltip_show(self, event): | |
78 | self.calltip.showtip("Hello world") | |
79 | ||
80 | def calltip_hide(self, event): | |
81 | self.calltip.hidetip() | |
82 | ||
83 | def main(): | |
84 | # Test code | |
85 | c=container() | |
86 | ||
87 | if __name__=='__main__': | |
88 | main() |