|
1 | | -"""A calltip window class for Tkinter/IDLE. |
| 1 | +"""A call-tip window class for Tkinter/IDLE. |
2 | 2 |
|
3 | | -After tooltip.py, which uses ideas gleaned from PySol |
4 | | -Used by calltip. |
| 3 | +After tooltip.py, which uses ideas gleaned from PySol. |
| 4 | +Used by calltip.py. |
5 | 5 | """ |
6 | | -from tkinter import Toplevel, Label, LEFT, SOLID, TclError |
| 6 | +from tkinter import Label, LEFT, SOLID, TclError |
7 | 7 |
|
8 | | -HIDE_VIRTUAL_EVENT_NAME = "<<calltipwindow-hide>>" |
| 8 | +from idlelib.tooltip import TooltipBase |
| 9 | + |
| 10 | +HIDE_EVENT = "<<calltipwindow-hide>>" |
9 | 11 | HIDE_SEQUENCES = ("<Key-Escape>", "<FocusOut>") |
10 | | -CHECKHIDE_VIRTUAL_EVENT_NAME = "<<calltipwindow-checkhide>>" |
| 12 | +CHECKHIDE_EVENT = "<<calltipwindow-checkhide>>" |
11 | 13 | CHECKHIDE_SEQUENCES = ("<KeyRelease>", "<ButtonRelease>") |
12 | | -CHECKHIDE_TIME = 100 # milliseconds |
| 14 | +CHECKHIDE_TIME = 100 # milliseconds |
13 | 15 |
|
14 | 16 | MARK_RIGHT = "calltipwindowregion_right" |
15 | 17 |
|
16 | | -class CalltipWindow: |
17 | 18 |
|
18 | | - def __init__(self, widget): |
19 | | - self.widget = widget |
20 | | - self.tipwindow = self.label = None |
21 | | - self.parenline = self.parencol = None |
22 | | - self.lastline = None |
| 19 | +class CalltipWindow(TooltipBase): |
| 20 | + """A call-tip widget for tkinter text widgets.""" |
| 21 | + |
| 22 | + def __init__(self, text_widget): |
| 23 | + """Create a call-tip; shown by showtip(). |
| 24 | +
|
| 25 | + text_widget: a Text widget with code for which call-tips are desired |
| 26 | + """ |
| 27 | + # Note: The Text widget will be accessible as self.anchor_widget |
| 28 | + super(CalltipWindow, self).__init__(text_widget) |
| 29 | + |
| 30 | + self.label = self.text = None |
| 31 | + self.parenline = self.parencol = self.lastline = None |
23 | 32 | self.hideid = self.checkhideid = None |
24 | 33 | self.checkhide_after_id = None |
25 | 34 |
|
26 | | - def position_window(self): |
27 | | - """Check if needs to reposition the window, and if so - do it.""" |
28 | | - curline = int(self.widget.index("insert").split('.')[0]) |
29 | | - if curline == self.lastline: |
30 | | - return |
31 | | - self.lastline = curline |
32 | | - self.widget.see("insert") |
| 35 | + def get_position(self): |
| 36 | + """Choose the position of the call-tip.""" |
| 37 | + curline = int(self.anchor_widget.index("insert").split('.')[0]) |
33 | 38 | if curline == self.parenline: |
34 | | - box = self.widget.bbox("%d.%d" % (self.parenline, |
35 | | - self.parencol)) |
| 39 | + anchor_index = (self.parenline, self.parencol) |
36 | 40 | else: |
37 | | - box = self.widget.bbox("%d.0" % curline) |
| 41 | + anchor_index = (curline, 0) |
| 42 | + box = self.anchor_widget.bbox("%d.%d" % anchor_index) |
38 | 43 | if not box: |
39 | | - box = list(self.widget.bbox("insert")) |
| 44 | + box = list(self.anchor_widget.bbox("insert")) |
40 | 45 | # align to left of window |
41 | 46 | box[0] = 0 |
42 | 47 | box[2] = 0 |
43 | | - x = box[0] + self.widget.winfo_rootx() + 2 |
44 | | - y = box[1] + box[3] + self.widget.winfo_rooty() |
45 | | - self.tipwindow.wm_geometry("+%d+%d" % (x, y)) |
| 48 | + return box[0] + 2, box[1] + box[3] |
| 49 | + |
| 50 | + def position_window(self): |
| 51 | + "Reposition the window if needed." |
| 52 | + curline = int(self.anchor_widget.index("insert").split('.')[0]) |
| 53 | + if curline == self.lastline: |
| 54 | + return |
| 55 | + self.lastline = curline |
| 56 | + self.anchor_widget.see("insert") |
| 57 | + super(CalltipWindow, self).position_window() |
46 | 58 |
|
47 | 59 | def showtip(self, text, parenleft, parenright): |
48 | | - """Show the calltip, bind events which will close it and reposition it. |
| 60 | + """Show the call-tip, bind events which will close it and reposition it. |
| 61 | +
|
| 62 | + text: the text to display in the call-tip |
| 63 | + parenleft: index of the opening parenthesis in the text widget |
| 64 | + parenright: index of the closing parenthesis in the text widget, |
| 65 | + or the end of the line if there is no closing parenthesis |
49 | 66 | """ |
50 | 67 | # Only called in calltip.Calltip, where lines are truncated |
51 | 68 | self.text = text |
52 | 69 | if self.tipwindow or not self.text: |
53 | 70 | return |
54 | 71 |
|
55 | | - self.widget.mark_set(MARK_RIGHT, parenright) |
| 72 | + self.anchor_widget.mark_set(MARK_RIGHT, parenright) |
56 | 73 | self.parenline, self.parencol = map( |
57 | | - int, self.widget.index(parenleft).split(".")) |
| 74 | + int, self.anchor_widget.index(parenleft).split(".")) |
58 | 75 |
|
59 | | - self.tipwindow = tw = Toplevel(self.widget) |
60 | | - self.position_window() |
61 | | - # remove border on calltip window |
62 | | - tw.wm_overrideredirect(1) |
63 | | - try: |
64 | | - # This command is only needed and available on Tk >= 8.4.0 for OSX |
65 | | - # Without it, call tips intrude on the typing process by grabbing |
66 | | - # the focus. |
67 | | - tw.tk.call("::tk::unsupported::MacWindowStyle", "style", tw._w, |
68 | | - "help", "noActivates") |
69 | | - except TclError: |
70 | | - pass |
71 | | - self.label = Label(tw, text=self.text, justify=LEFT, |
| 76 | + super(CalltipWindow, self).showtip() |
| 77 | + |
| 78 | + self._bind_events() |
| 79 | + |
| 80 | + def showcontents(self): |
| 81 | + """Create the call-tip widget.""" |
| 82 | + self.label = Label(self.tipwindow, text=self.text, justify=LEFT, |
72 | 83 | background="#ffffe0", relief=SOLID, borderwidth=1, |
73 | | - font = self.widget['font']) |
| 84 | + font=self.anchor_widget['font']) |
74 | 85 | self.label.pack() |
75 | | - tw.update_idletasks() |
76 | | - tw.lift() # work around bug in Tk 8.5.18+ (issue #24570) |
77 | | - |
78 | | - self.checkhideid = self.widget.bind(CHECKHIDE_VIRTUAL_EVENT_NAME, |
79 | | - self.checkhide_event) |
80 | | - for seq in CHECKHIDE_SEQUENCES: |
81 | | - self.widget.event_add(CHECKHIDE_VIRTUAL_EVENT_NAME, seq) |
82 | | - self.widget.after(CHECKHIDE_TIME, self.checkhide_event) |
83 | | - self.hideid = self.widget.bind(HIDE_VIRTUAL_EVENT_NAME, |
84 | | - self.hide_event) |
85 | | - for seq in HIDE_SEQUENCES: |
86 | | - self.widget.event_add(HIDE_VIRTUAL_EVENT_NAME, seq) |
87 | 86 |
|
88 | 87 | def checkhide_event(self, event=None): |
| 88 | + """Handle CHECK_HIDE_EVENT: call hidetip or reschedule.""" |
89 | 89 | if not self.tipwindow: |
90 | | - # If the event was triggered by the same event that unbinded |
| 90 | + # If the event was triggered by the same event that unbound |
91 | 91 | # this function, the function will be called nevertheless, |
92 | 92 | # so do nothing in this case. |
93 | 93 | return None |
94 | | - curline, curcol = map(int, self.widget.index("insert").split('.')) |
| 94 | + |
| 95 | + # Hide the call-tip if the insertion cursor moves outside of the |
| 96 | + # parenthesis. |
| 97 | + curline, curcol = map(int, self.anchor_widget.index("insert").split('.')) |
95 | 98 | if curline < self.parenline or \ |
96 | 99 | (curline == self.parenline and curcol <= self.parencol) or \ |
97 | | - self.widget.compare("insert", ">", MARK_RIGHT): |
| 100 | + self.anchor_widget.compare("insert", ">", MARK_RIGHT): |
98 | 101 | self.hidetip() |
99 | 102 | return "break" |
100 | | - else: |
101 | | - self.position_window() |
102 | | - if self.checkhide_after_id is not None: |
103 | | - self.widget.after_cancel(self.checkhide_after_id) |
104 | | - self.checkhide_after_id = \ |
105 | | - self.widget.after(CHECKHIDE_TIME, self.checkhide_event) |
106 | | - return None |
| 103 | + |
| 104 | + # Not hiding the call-tip. |
| 105 | + |
| 106 | + self.position_window() |
| 107 | + # Re-schedule this function to be called again in a short while. |
| 108 | + if self.checkhide_after_id is not None: |
| 109 | + self.anchor_widget.after_cancel(self.checkhide_after_id) |
| 110 | + self.checkhide_after_id = \ |
| 111 | + self.anchor_widget.after(CHECKHIDE_TIME, self.checkhide_event) |
| 112 | + return None |
107 | 113 |
|
108 | 114 | def hide_event(self, event): |
| 115 | + """Handle HIDE_EVENT by calling hidetip.""" |
109 | 116 | if not self.tipwindow: |
110 | 117 | # See the explanation in checkhide_event. |
111 | 118 | return None |
112 | 119 | self.hidetip() |
113 | 120 | return "break" |
114 | 121 |
|
115 | 122 | def hidetip(self): |
| 123 | + """Hide the call-tip.""" |
116 | 124 | if not self.tipwindow: |
117 | 125 | return |
118 | 126 |
|
119 | | - for seq in CHECKHIDE_SEQUENCES: |
120 | | - self.widget.event_delete(CHECKHIDE_VIRTUAL_EVENT_NAME, seq) |
121 | | - self.widget.unbind(CHECKHIDE_VIRTUAL_EVENT_NAME, self.checkhideid) |
122 | | - self.checkhideid = None |
123 | | - for seq in HIDE_SEQUENCES: |
124 | | - self.widget.event_delete(HIDE_VIRTUAL_EVENT_NAME, seq) |
125 | | - self.widget.unbind(HIDE_VIRTUAL_EVENT_NAME, self.hideid) |
126 | | - self.hideid = None |
127 | | - |
128 | | - self.label.destroy() |
| 127 | + try: |
| 128 | + self.label.destroy() |
| 129 | + except TclError: |
| 130 | + pass |
129 | 131 | self.label = None |
130 | | - self.tipwindow.destroy() |
131 | | - self.tipwindow = None |
132 | 132 |
|
133 | | - self.widget.mark_unset(MARK_RIGHT) |
134 | 133 | self.parenline = self.parencol = self.lastline = None |
| 134 | + try: |
| 135 | + self.anchor_widget.mark_unset(MARK_RIGHT) |
| 136 | + except TclError: |
| 137 | + pass |
135 | 138 |
|
136 | | - def is_active(self): |
137 | | - return bool(self.tipwindow) |
| 139 | + try: |
| 140 | + self._unbind_events() |
| 141 | + except (TclError, ValueError): |
| 142 | + # ValueError may be raised by MultiCall |
| 143 | + pass |
| 144 | + |
| 145 | + super(CalltipWindow, self).hidetip() |
| 146 | + |
| 147 | + def _bind_events(self): |
| 148 | + """Bind event handlers.""" |
| 149 | + self.checkhideid = self.anchor_widget.bind(CHECKHIDE_EVENT, |
| 150 | + self.checkhide_event) |
| 151 | + for seq in CHECKHIDE_SEQUENCES: |
| 152 | + self.anchor_widget.event_add(CHECKHIDE_EVENT, seq) |
| 153 | + self.anchor_widget.after(CHECKHIDE_TIME, self.checkhide_event) |
| 154 | + self.hideid = self.anchor_widget.bind(HIDE_EVENT, |
| 155 | + self.hide_event) |
| 156 | + for seq in HIDE_SEQUENCES: |
| 157 | + self.anchor_widget.event_add(HIDE_EVENT, seq) |
| 158 | + |
| 159 | + def _unbind_events(self): |
| 160 | + """Unbind event handlers.""" |
| 161 | + for seq in CHECKHIDE_SEQUENCES: |
| 162 | + self.anchor_widget.event_delete(CHECKHIDE_EVENT, seq) |
| 163 | + self.anchor_widget.unbind(CHECKHIDE_EVENT, self.checkhideid) |
| 164 | + self.checkhideid = None |
| 165 | + for seq in HIDE_SEQUENCES: |
| 166 | + self.anchor_widget.event_delete(HIDE_EVENT, seq) |
| 167 | + self.anchor_widget.unbind(HIDE_EVENT, self.hideid) |
| 168 | + self.hideid = None |
138 | 169 |
|
139 | 170 |
|
140 | 171 | def _calltip_window(parent): # htest # |
141 | 172 | from tkinter import Toplevel, Text, LEFT, BOTH |
142 | 173 |
|
143 | 174 | top = Toplevel(parent) |
144 | | - top.title("Test calltips") |
| 175 | + top.title("Test call-tips") |
145 | 176 | x, y = map(int, parent.geometry().split('+')[1:]) |
146 | | - top.geometry("200x100+%d+%d" % (x + 250, y + 175)) |
| 177 | + top.geometry("250x100+%d+%d" % (x + 175, y + 150)) |
147 | 178 | text = Text(top) |
148 | 179 | text.pack(side=LEFT, fill=BOTH, expand=1) |
149 | 180 | text.insert("insert", "string.split") |
150 | 181 | top.update() |
151 | | - calltip = CalltipWindow(text) |
152 | 182 |
|
| 183 | + calltip = CalltipWindow(text) |
153 | 184 | def calltip_show(event): |
154 | | - calltip.showtip("(s=Hello world)", "insert", "end") |
| 185 | + calltip.showtip("(s='Hello world')", "insert", "end") |
155 | 186 | def calltip_hide(event): |
156 | 187 | calltip.hidetip() |
157 | 188 | text.event_add("<<calltip-show>>", "(") |
158 | 189 | text.event_add("<<calltip-hide>>", ")") |
159 | 190 | text.bind("<<calltip-show>>", calltip_show) |
160 | 191 | text.bind("<<calltip-hide>>", calltip_hide) |
| 192 | + |
161 | 193 | text.focus_set() |
162 | 194 |
|
163 | 195 | if __name__ == '__main__': |
|
0 commit comments