Guido van Rossum | 85b9735 | 1999-06-02 16:10:19 +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. |
Guido van Rossum | 85b9735 | 1999-06-02 16:10:19 +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): |
Tim Peters | 32b069c | 2002-04-22 18:43:49 +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] + ' ...' |
Guido van Rossum | 85b9735 | 1999-06-02 16:10:19 +0000 | [diff] [blame] | 21 | self.text = text |
Tim Peters | 32b069c | 2002-04-22 18:43:49 +0000 | [diff] [blame] | 22 | |
Guido van Rossum | 85b9735 | 1999-06-02 16:10:19 +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)) |
| 32 | label = Label(tw, text=self.text, justify=LEFT, |
Guido van Rossum | ab3b50b | 1999-06-10 14:19:46 +0000 | [diff] [blame] | 33 | background="#ffffe0", relief=SOLID, borderwidth=1, |
| 34 | font = self.widget['font']) |
Guido van Rossum | 85b9735 | 1999-06-02 16:10:19 +0000 | [diff] [blame] | 35 | label.pack() |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 85b9735 | 1999-06-02 16:10:19 +0000 | [diff] [blame] | 37 | def hidetip(self): |
| 38 | tw = self.tipwindow |
| 39 | self.tipwindow = None |
| 40 | if tw: |
| 41 | tw.destroy() |
| 42 | |
| 43 | |
| 44 | ############################### |
| 45 | # |
| 46 | # Test Code |
| 47 | # |
| 48 | class container: # Conceptually an editor_window |
| 49 | def __init__(self): |
| 50 | root = Tk() |
| 51 | text = self.text = Text(root) |
| 52 | text.pack(side=LEFT, fill=BOTH, expand=1) |
| 53 | text.insert("insert", "string.split") |
| 54 | root.update() |
| 55 | self.calltip = CallTip(text) |
| 56 | |
| 57 | text.event_add("<<calltip-show>>", "(") |
| 58 | text.event_add("<<calltip-hide>>", ")") |
| 59 | text.bind("<<calltip-show>>", self.calltip_show) |
| 60 | text.bind("<<calltip-hide>>", self.calltip_hide) |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 61 | |
Guido van Rossum | 85b9735 | 1999-06-02 16:10:19 +0000 | [diff] [blame] | 62 | text.focus_set() |
| 63 | # root.mainloop() # not in idle |
| 64 | |
| 65 | def calltip_show(self, event): |
| 66 | self.calltip.showtip("Hello world") |
| 67 | |
| 68 | def calltip_hide(self, event): |
| 69 | self.calltip.hidetip() |
| 70 | |
| 71 | def main(): |
| 72 | # Test code |
| 73 | c=container() |
| 74 | |
| 75 | if __name__=='__main__': |
| 76 | main() |