| 1 | n/a | """A CallTip window class for Tkinter/IDLE. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | After ToolTip.py, which uses ideas gleaned from PySol |
|---|
| 4 | n/a | Used by the CallTips IDLE extension. |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | """ |
|---|
| 7 | n/a | from tkinter import * |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | HIDE_VIRTUAL_EVENT_NAME = "<<calltipwindow-hide>>" |
|---|
| 10 | n/a | HIDE_SEQUENCES = ("<Key-Escape>", "<FocusOut>") |
|---|
| 11 | n/a | CHECKHIDE_VIRTUAL_EVENT_NAME = "<<calltipwindow-checkhide>>" |
|---|
| 12 | n/a | CHECKHIDE_SEQUENCES = ("<KeyRelease>", "<ButtonRelease>") |
|---|
| 13 | n/a | CHECKHIDE_TIME = 100 # miliseconds |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | MARK_RIGHT = "calltipwindowregion_right" |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | class CallTip: |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | def __init__(self, widget): |
|---|
| 20 | n/a | self.widget = widget |
|---|
| 21 | n/a | self.tipwindow = self.label = None |
|---|
| 22 | n/a | self.parenline = self.parencol = None |
|---|
| 23 | n/a | self.lastline = None |
|---|
| 24 | n/a | self.hideid = self.checkhideid = None |
|---|
| 25 | n/a | self.checkhide_after_id = None |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | def position_window(self): |
|---|
| 28 | n/a | """Check if needs to reposition the window, and if so - do it.""" |
|---|
| 29 | n/a | curline = int(self.widget.index("insert").split('.')[0]) |
|---|
| 30 | n/a | if curline == self.lastline: |
|---|
| 31 | n/a | return |
|---|
| 32 | n/a | self.lastline = curline |
|---|
| 33 | n/a | self.widget.see("insert") |
|---|
| 34 | n/a | if curline == self.parenline: |
|---|
| 35 | n/a | box = self.widget.bbox("%d.%d" % (self.parenline, |
|---|
| 36 | n/a | self.parencol)) |
|---|
| 37 | n/a | else: |
|---|
| 38 | n/a | box = self.widget.bbox("%d.0" % curline) |
|---|
| 39 | n/a | if not box: |
|---|
| 40 | n/a | box = list(self.widget.bbox("insert")) |
|---|
| 41 | n/a | # align to left of window |
|---|
| 42 | n/a | box[0] = 0 |
|---|
| 43 | n/a | box[2] = 0 |
|---|
| 44 | n/a | x = box[0] + self.widget.winfo_rootx() + 2 |
|---|
| 45 | n/a | y = box[1] + box[3] + self.widget.winfo_rooty() |
|---|
| 46 | n/a | self.tipwindow.wm_geometry("+%d+%d" % (x, y)) |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | def showtip(self, text, parenleft, parenright): |
|---|
| 49 | n/a | """Show the calltip, bind events which will close it and reposition it. |
|---|
| 50 | n/a | """ |
|---|
| 51 | n/a | # truncate overly long calltip |
|---|
| 52 | n/a | if len(text) >= 79: |
|---|
| 53 | n/a | textlines = text.splitlines() |
|---|
| 54 | n/a | for i, line in enumerate(textlines): |
|---|
| 55 | n/a | if len(line) > 79: |
|---|
| 56 | n/a | textlines[i] = line[:75] + ' ...' |
|---|
| 57 | n/a | text = '\n'.join(textlines) |
|---|
| 58 | n/a | self.text = text |
|---|
| 59 | n/a | if self.tipwindow or not self.text: |
|---|
| 60 | n/a | return |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | self.widget.mark_set(MARK_RIGHT, parenright) |
|---|
| 63 | n/a | self.parenline, self.parencol = map( |
|---|
| 64 | n/a | int, self.widget.index(parenleft).split(".")) |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | self.tipwindow = tw = Toplevel(self.widget) |
|---|
| 67 | n/a | self.position_window() |
|---|
| 68 | n/a | # remove border on calltip window |
|---|
| 69 | n/a | tw.wm_overrideredirect(1) |
|---|
| 70 | n/a | try: |
|---|
| 71 | n/a | # This command is only needed and available on Tk >= 8.4.0 for OSX |
|---|
| 72 | n/a | # Without it, call tips intrude on the typing process by grabbing |
|---|
| 73 | n/a | # the focus. |
|---|
| 74 | n/a | tw.tk.call("::tk::unsupported::MacWindowStyle", "style", tw._w, |
|---|
| 75 | n/a | "help", "noActivates") |
|---|
| 76 | n/a | except TclError: |
|---|
| 77 | n/a | pass |
|---|
| 78 | n/a | self.label = Label(tw, text=self.text, justify=LEFT, |
|---|
| 79 | n/a | background="#ffffe0", relief=SOLID, borderwidth=1, |
|---|
| 80 | n/a | font = self.widget['font']) |
|---|
| 81 | n/a | self.label.pack() |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | self.checkhideid = self.widget.bind(CHECKHIDE_VIRTUAL_EVENT_NAME, |
|---|
| 84 | n/a | self.checkhide_event) |
|---|
| 85 | n/a | for seq in CHECKHIDE_SEQUENCES: |
|---|
| 86 | n/a | self.widget.event_add(CHECKHIDE_VIRTUAL_EVENT_NAME, seq) |
|---|
| 87 | n/a | self.widget.after(CHECKHIDE_TIME, self.checkhide_event) |
|---|
| 88 | n/a | self.hideid = self.widget.bind(HIDE_VIRTUAL_EVENT_NAME, |
|---|
| 89 | n/a | self.hide_event) |
|---|
| 90 | n/a | for seq in HIDE_SEQUENCES: |
|---|
| 91 | n/a | self.widget.event_add(HIDE_VIRTUAL_EVENT_NAME, seq) |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | def checkhide_event(self, event=None): |
|---|
| 94 | n/a | if not self.tipwindow: |
|---|
| 95 | n/a | # If the event was triggered by the same event that unbinded |
|---|
| 96 | n/a | # this function, the function will be called nevertheless, |
|---|
| 97 | n/a | # so do nothing in this case. |
|---|
| 98 | n/a | return |
|---|
| 99 | n/a | curline, curcol = map(int, self.widget.index("insert").split('.')) |
|---|
| 100 | n/a | if curline < self.parenline or \ |
|---|
| 101 | n/a | (curline == self.parenline and curcol <= self.parencol) or \ |
|---|
| 102 | n/a | self.widget.compare("insert", ">", MARK_RIGHT): |
|---|
| 103 | n/a | self.hidetip() |
|---|
| 104 | n/a | else: |
|---|
| 105 | n/a | self.position_window() |
|---|
| 106 | n/a | if self.checkhide_after_id is not None: |
|---|
| 107 | n/a | self.widget.after_cancel(self.checkhide_after_id) |
|---|
| 108 | n/a | self.checkhide_after_id = \ |
|---|
| 109 | n/a | self.widget.after(CHECKHIDE_TIME, self.checkhide_event) |
|---|
| 110 | n/a | |
|---|
| 111 | n/a | def hide_event(self, event): |
|---|
| 112 | n/a | if not self.tipwindow: |
|---|
| 113 | n/a | # See the explanation in checkhide_event. |
|---|
| 114 | n/a | return |
|---|
| 115 | n/a | self.hidetip() |
|---|
| 116 | n/a | |
|---|
| 117 | n/a | def hidetip(self): |
|---|
| 118 | n/a | if not self.tipwindow: |
|---|
| 119 | n/a | return |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | for seq in CHECKHIDE_SEQUENCES: |
|---|
| 122 | n/a | self.widget.event_delete(CHECKHIDE_VIRTUAL_EVENT_NAME, seq) |
|---|
| 123 | n/a | self.widget.unbind(CHECKHIDE_VIRTUAL_EVENT_NAME, self.checkhideid) |
|---|
| 124 | n/a | self.checkhideid = None |
|---|
| 125 | n/a | for seq in HIDE_SEQUENCES: |
|---|
| 126 | n/a | self.widget.event_delete(HIDE_VIRTUAL_EVENT_NAME, seq) |
|---|
| 127 | n/a | self.widget.unbind(HIDE_VIRTUAL_EVENT_NAME, self.hideid) |
|---|
| 128 | n/a | self.hideid = None |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | self.label.destroy() |
|---|
| 131 | n/a | self.label = None |
|---|
| 132 | n/a | self.tipwindow.destroy() |
|---|
| 133 | n/a | self.tipwindow = None |
|---|
| 134 | n/a | |
|---|
| 135 | n/a | self.widget.mark_unset(MARK_RIGHT) |
|---|
| 136 | n/a | self.parenline = self.parencol = self.lastline = None |
|---|
| 137 | n/a | |
|---|
| 138 | n/a | def is_active(self): |
|---|
| 139 | n/a | return bool(self.tipwindow) |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | |
|---|
| 142 | n/a | |
|---|
| 143 | n/a | ############################### |
|---|
| 144 | n/a | # |
|---|
| 145 | n/a | # Test Code |
|---|
| 146 | n/a | # |
|---|
| 147 | n/a | class container: # Conceptually an editor_window |
|---|
| 148 | n/a | def __init__(self): |
|---|
| 149 | n/a | root = Tk() |
|---|
| 150 | n/a | text = self.text = Text(root) |
|---|
| 151 | n/a | text.pack(side=LEFT, fill=BOTH, expand=1) |
|---|
| 152 | n/a | text.insert("insert", "string.split") |
|---|
| 153 | n/a | root.update() |
|---|
| 154 | n/a | self.calltip = CallTip(text) |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | text.event_add("<<calltip-show>>", "(") |
|---|
| 157 | n/a | text.event_add("<<calltip-hide>>", ")") |
|---|
| 158 | n/a | text.bind("<<calltip-show>>", self.calltip_show) |
|---|
| 159 | n/a | text.bind("<<calltip-hide>>", self.calltip_hide) |
|---|
| 160 | n/a | |
|---|
| 161 | n/a | text.focus_set() |
|---|
| 162 | n/a | root.mainloop() |
|---|
| 163 | n/a | |
|---|
| 164 | n/a | def calltip_show(self, event): |
|---|
| 165 | n/a | self.calltip.showtip("Hello world") |
|---|
| 166 | n/a | |
|---|
| 167 | n/a | def calltip_hide(self, event): |
|---|
| 168 | n/a | self.calltip.hidetip() |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | def main(): |
|---|
| 171 | n/a | # Test code |
|---|
| 172 | n/a | c=container() |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | if __name__=='__main__': |
|---|
| 175 | n/a | main() |
|---|