David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1 | # 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. |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 5 | from Tkinter import * |
| 6 | |
| 7 | class CallTip: |
| 8 | |
| 9 | def __init__(self, widget): |
| 10 | self.widget = widget |
| 11 | self.tipwindow = None |
| 12 | self.id = None |
| 13 | self.x = self.y = 0 |
| 14 | |
| 15 | def showtip(self, text): |
Kurt B. Kaiser | e72f05d | 2002-09-15 21:43:13 +0000 | [diff] [blame] | 16 | # SF bug 546078: IDLE calltips cause application error. |
| 17 | # There were crashes on various Windows flavors, and even a |
| 18 | # crashing X server on Linux, with very long calltips. |
| 19 | if len(text) >= 79: |
| 20 | text = text[:75] + ' ...' |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 21 | self.text = text |
Kurt B. Kaiser | e72f05d | 2002-09-15 21:43:13 +0000 | [diff] [blame] | 22 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 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 | tw.wm_overrideredirect(1) |
| 31 | tw.wm_geometry("+%d+%d" % (x, y)) |
Tony Lownds | 8b1b8d6 | 2002-09-23 01:04:05 +0000 | [diff] [blame^] | 32 | try: |
| 33 | # This command is only needed and available on Tk >= 8.4.0 for OSX |
| 34 | # Without it, call tips intrude on the typing process by grabbing |
| 35 | # the focus. |
| 36 | tw.tk.call("::tk::unsupported::MacWindowStyle", "style", tw._w, |
| 37 | "help", "noActivates") |
| 38 | except TclError: |
| 39 | pass |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 40 | label = Label(tw, text=self.text, justify=LEFT, |
| 41 | background="#ffffe0", relief=SOLID, borderwidth=1, |
| 42 | font = self.widget['font']) |
| 43 | label.pack() |
Kurt B. Kaiser | 9a1ae1a | 2001-07-12 22:26:44 +0000 | [diff] [blame] | 44 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 45 | def hidetip(self): |
| 46 | tw = self.tipwindow |
| 47 | self.tipwindow = None |
| 48 | if tw: |
| 49 | tw.destroy() |
| 50 | |
| 51 | |
| 52 | ############################### |
| 53 | # |
| 54 | # Test Code |
| 55 | # |
| 56 | class container: # Conceptually an editor_window |
| 57 | def __init__(self): |
| 58 | root = Tk() |
| 59 | text = self.text = Text(root) |
| 60 | text.pack(side=LEFT, fill=BOTH, expand=1) |
| 61 | text.insert("insert", "string.split") |
| 62 | root.update() |
| 63 | self.calltip = CallTip(text) |
| 64 | |
| 65 | text.event_add("<<calltip-show>>", "(") |
| 66 | text.event_add("<<calltip-hide>>", ")") |
| 67 | text.bind("<<calltip-show>>", self.calltip_show) |
| 68 | text.bind("<<calltip-hide>>", self.calltip_hide) |
Kurt B. Kaiser | 9a1ae1a | 2001-07-12 22:26:44 +0000 | [diff] [blame] | 69 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 70 | text.focus_set() |
| 71 | # root.mainloop() # not in idle |
| 72 | |
| 73 | def calltip_show(self, event): |
| 74 | self.calltip.showtip("Hello world") |
| 75 | |
| 76 | def calltip_hide(self, event): |
| 77 | self.calltip.hidetip() |
| 78 | |
| 79 | def main(): |
| 80 | # Test code |
| 81 | c=container() |
| 82 | |
| 83 | if __name__=='__main__': |
| 84 | main() |