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. |
| 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 | font = self.widget['font']) |
| 30 | label.pack() |
Kurt B. Kaiser | 9a1ae1a | 2001-07-12 22:26:44 +0000 | [diff] [blame^] | 31 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 32 | def hidetip(self): |
| 33 | tw = self.tipwindow |
| 34 | self.tipwindow = None |
| 35 | if tw: |
| 36 | tw.destroy() |
| 37 | |
| 38 | |
| 39 | ############################### |
| 40 | # |
| 41 | # Test Code |
| 42 | # |
| 43 | class container: # Conceptually an editor_window |
| 44 | def __init__(self): |
| 45 | root = Tk() |
| 46 | text = self.text = Text(root) |
| 47 | text.pack(side=LEFT, fill=BOTH, expand=1) |
| 48 | text.insert("insert", "string.split") |
| 49 | root.update() |
| 50 | self.calltip = CallTip(text) |
| 51 | |
| 52 | text.event_add("<<calltip-show>>", "(") |
| 53 | text.event_add("<<calltip-hide>>", ")") |
| 54 | text.bind("<<calltip-show>>", self.calltip_show) |
| 55 | text.bind("<<calltip-hide>>", self.calltip_hide) |
Kurt B. Kaiser | 9a1ae1a | 2001-07-12 22:26:44 +0000 | [diff] [blame^] | 56 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 57 | text.focus_set() |
| 58 | # root.mainloop() # not in idle |
| 59 | |
| 60 | def calltip_show(self, event): |
| 61 | self.calltip.showtip("Hello world") |
| 62 | |
| 63 | def calltip_hide(self, event): |
| 64 | self.calltip.hidetip() |
| 65 | |
| 66 | def main(): |
| 67 | # Test code |
| 68 | c=container() |
| 69 | |
| 70 | if __name__=='__main__': |
| 71 | main() |