Kurt B. Kaiser | e54710b | 2002-12-12 19:15:39 +0000 | [diff] [blame^] | 1 | """A CallTip window class for Tkinter/IDLE. |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 2 | |
Kurt B. Kaiser | e54710b | 2002-12-12 19:15:39 +0000 | [diff] [blame^] | 3 | After ToolTip.py, which uses ideas gleaned from PySol |
| 4 | Used by the CallTips IDLE extension. |
| 5 | |
| 6 | """ |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 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): |
Kurt B. Kaiser | e54710b | 2002-12-12 19:15:39 +0000 | [diff] [blame^] | 18 | " Display text in calltip window" |
| 19 | # truncate overly long calltip |
Kurt B. Kaiser | e72f05d | 2002-09-15 21:43:13 +0000 | [diff] [blame] | 20 | if len(text) >= 79: |
| 21 | text = text[:75] + ' ...' |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 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) |
Kurt B. Kaiser | e54710b | 2002-12-12 19:15:39 +0000 | [diff] [blame^] | 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! |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 34 | tw.wm_overrideredirect(1) |
| 35 | tw.wm_geometry("+%d+%d" % (x, y)) |
Tony Lownds | 8b1b8d6 | 2002-09-23 01:04:05 +0000 | [diff] [blame] | 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 |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 44 | label = Label(tw, text=self.text, justify=LEFT, |
| 45 | background="#ffffe0", relief=SOLID, borderwidth=1, |
| 46 | font = self.widget['font']) |
| 47 | label.pack() |
Kurt B. Kaiser | 9a1ae1a | 2001-07-12 22:26:44 +0000 | [diff] [blame] | 48 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 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) |
Kurt B. Kaiser | 9a1ae1a | 2001-07-12 22:26:44 +0000 | [diff] [blame] | 73 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 74 | text.focus_set() |
Kurt B. Kaiser | e54710b | 2002-12-12 19:15:39 +0000 | [diff] [blame^] | 75 | root.mainloop() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 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() |