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. |
| 5 | import os |
| 6 | from Tkinter import * |
| 7 | |
| 8 | class 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, |
| 28 | background="#ffffe0", relief=SOLID, borderwidth=1) |
| 29 | label.pack() |
| 30 | |
| 31 | def hidetip(self): |
| 32 | tw = self.tipwindow |
| 33 | self.tipwindow = None |
| 34 | if tw: |
| 35 | tw.destroy() |
| 36 | |
| 37 | |
| 38 | ############################### |
| 39 | # |
| 40 | # Test Code |
| 41 | # |
| 42 | class container: # Conceptually an editor_window |
| 43 | def __init__(self): |
| 44 | root = Tk() |
| 45 | text = self.text = Text(root) |
| 46 | text.pack(side=LEFT, fill=BOTH, expand=1) |
| 47 | text.insert("insert", "string.split") |
| 48 | root.update() |
| 49 | self.calltip = CallTip(text) |
| 50 | |
| 51 | text.event_add("<<calltip-show>>", "(") |
| 52 | text.event_add("<<calltip-hide>>", ")") |
| 53 | text.bind("<<calltip-show>>", self.calltip_show) |
| 54 | text.bind("<<calltip-hide>>", self.calltip_hide) |
| 55 | |
| 56 | text.focus_set() |
| 57 | # root.mainloop() # not in idle |
| 58 | |
| 59 | def calltip_show(self, event): |
| 60 | self.calltip.showtip("Hello world") |
| 61 | |
| 62 | def calltip_hide(self, event): |
| 63 | self.calltip.hidetip() |
| 64 | |
| 65 | def main(): |
| 66 | # Test code |
| 67 | c=container() |
| 68 | |
| 69 | if __name__=='__main__': |
| 70 | main() |