blob: c69ab4a368363021887d9c6baff0c468a753ae16 [file] [log] [blame]
Kurt B. Kaiserb1754452005-11-18 22:05:48 +00001"""
Terry Jan Reedy6fa5bdc2016-05-28 13:22:31 -04002An auto-completion window for IDLE, used by the autocomplete extension
Kurt B. Kaiserb1754452005-11-18 22:05:48 +00003"""
mlouielu778b4842017-06-14 23:13:19 +08004import platform
5
Georg Brandl14fc4272008-05-17 18:39:55 +00006from tkinter import *
Terry Jan Reedyaff0ada2019-01-02 22:04:06 -05007from tkinter.ttk import Frame, Scrollbar
Terry Jan Reedybfbaa6b2016-08-31 00:50:55 -04008
Terry Jan Reedy12131232019-08-04 19:48:52 -04009from idlelib.autocomplete import FILES, ATTRS
Terry Jan Reedybfbaa6b2016-08-31 00:50:55 -040010from idlelib.multicall import MC_SHIFT
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000011
12HIDE_VIRTUAL_EVENT_NAME = "<<autocompletewindow-hide>>"
mlouielu778b4842017-06-14 23:13:19 +080013HIDE_FOCUS_OUT_SEQUENCE = "<FocusOut>"
14HIDE_SEQUENCES = (HIDE_FOCUS_OUT_SEQUENCE, "<ButtonPress>")
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000015KEYPRESS_VIRTUAL_EVENT_NAME = "<<autocompletewindow-keypress>>"
16# We need to bind event beyond <Key> so that the function will be called
17# before the default specific IDLE function
Thomas Wouterscf297e42007-02-23 15:07:44 +000018KEYPRESS_SEQUENCES = ("<Key>", "<Key-BackSpace>", "<Key-Return>", "<Key-Tab>",
19 "<Key-Up>", "<Key-Down>", "<Key-Home>", "<Key-End>",
20 "<Key-Prior>", "<Key-Next>")
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000021KEYRELEASE_VIRTUAL_EVENT_NAME = "<<autocompletewindow-keyrelease>>"
22KEYRELEASE_SEQUENCE = "<KeyRelease>"
Thomas Wouterscf297e42007-02-23 15:07:44 +000023LISTUPDATE_SEQUENCE = "<B1-ButtonRelease>"
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000024WINCONFIG_SEQUENCE = "<Configure>"
Thomas Wouterscf297e42007-02-23 15:07:44 +000025DOUBLECLICK_SEQUENCE = "<B1-Double-ButtonRelease>"
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000026
27class AutoCompleteWindow:
28
29 def __init__(self, widget):
30 # The widget (Text) on which we place the AutoCompleteWindow
31 self.widget = widget
32 # The widgets we create
33 self.autocompletewindow = self.listbox = self.scrollbar = None
34 # The default foreground and background of a selection. Saved because
35 # they are changed to the regular colors of list items when the
36 # completion start is not a prefix of the selected completion
37 self.origselforeground = self.origselbackground = None
38 # The list of completions
39 self.completions = None
40 # A list with more completions, or None
41 self.morecompletions = None
Terry Jan Reedy12131232019-08-04 19:48:52 -040042 # The completion mode, either autocomplete.ATTRS or .FILES.
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000043 self.mode = None
44 # The current completion start, on the text box (a string)
45 self.start = None
46 # The index of the start of the completion
47 self.startindex = None
48 # The last typed start, used so that when the selection changes,
49 # the new start will be as close as possible to the last typed one.
50 self.lasttypedstart = None
51 # Do we have an indication that the user wants the completion window
52 # (for example, he clicked the list)
53 self.userwantswindow = None
54 # event ids
55 self.hideid = self.keypressid = self.listupdateid = self.winconfigid \
56 = self.keyreleaseid = self.doubleclickid = None
Thomas Wouterscf297e42007-02-23 15:07:44 +000057 # Flag set if last keypress was a tab
58 self.lastkey_was_tab = False
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000059
60 def _change_start(self, newstart):
Christian Heimes81ee3ef2008-05-04 22:42:01 +000061 min_len = min(len(self.start), len(newstart))
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000062 i = 0
Christian Heimes81ee3ef2008-05-04 22:42:01 +000063 while i < min_len and self.start[i] == newstart[i]:
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000064 i += 1
65 if i < len(self.start):
66 self.widget.delete("%s+%dc" % (self.startindex, i),
67 "%s+%dc" % (self.startindex, len(self.start)))
68 if i < len(newstart):
69 self.widget.insert("%s+%dc" % (self.startindex, i),
70 newstart[i:])
71 self.start = newstart
72
73 def _binary_search(self, s):
74 """Find the first index in self.completions where completions[i] is
Terry Jan Reedy12131232019-08-04 19:48:52 -040075 greater or equal to s, or the last index if there is no such.
76 """
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000077 i = 0; j = len(self.completions)
78 while j > i:
79 m = (i + j) // 2
80 if self.completions[m] >= s:
81 j = m
82 else:
83 i = m + 1
84 return min(i, len(self.completions)-1)
85
86 def _complete_string(self, s):
87 """Assuming that s is the prefix of a string in self.completions,
88 return the longest string which is a prefix of all the strings which
Terry Jan Reedy12131232019-08-04 19:48:52 -040089 s is a prefix of them. If s is not a prefix of a string, return s.
90 """
Kurt B. Kaiserb1754452005-11-18 22:05:48 +000091 first = self._binary_search(s)
92 if self.completions[first][:len(s)] != s:
93 # There is not even one completion which s is a prefix of.
94 return s
95 # Find the end of the range of completions where s is a prefix of.
96 i = first + 1
97 j = len(self.completions)
98 while j > i:
99 m = (i + j) // 2
100 if self.completions[m][:len(s)] != s:
101 j = m
102 else:
103 i = m + 1
104 last = i-1
105
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000106 if first == last: # only one possible completion
107 return self.completions[first]
108
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000109 # We should return the maximum prefix of first and last
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000110 first_comp = self.completions[first]
111 last_comp = self.completions[last]
112 min_len = min(len(first_comp), len(last_comp))
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000113 i = len(s)
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000114 while i < min_len and first_comp[i] == last_comp[i]:
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000115 i += 1
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000116 return first_comp[:i]
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000117
118 def _selection_changed(self):
Terry Jan Reedy12131232019-08-04 19:48:52 -0400119 """Call when the selection of the Listbox has changed.
120
121 Updates the Listbox display and calls _change_start.
122 """
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000123 cursel = int(self.listbox.curselection()[0])
124
125 self.listbox.see(cursel)
126
127 lts = self.lasttypedstart
128 selstart = self.completions[cursel]
129 if self._binary_search(lts) == cursel:
130 newstart = lts
131 else:
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000132 min_len = min(len(lts), len(selstart))
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000133 i = 0
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000134 while i < min_len and lts[i] == selstart[i]:
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000135 i += 1
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000136 newstart = selstart[:i]
137 self._change_start(newstart)
138
139 if self.completions[cursel][:len(self.start)] == self.start:
140 # start is a prefix of the selected completion
141 self.listbox.configure(selectbackground=self.origselbackground,
142 selectforeground=self.origselforeground)
143 else:
144 self.listbox.configure(selectbackground=self.listbox.cget("bg"),
145 selectforeground=self.listbox.cget("fg"))
146 # If there are more completions, show them, and call me again.
147 if self.morecompletions:
148 self.completions = self.morecompletions
149 self.morecompletions = None
150 self.listbox.delete(0, END)
151 for item in self.completions:
152 self.listbox.insert(END, item)
153 self.listbox.select_set(self._binary_search(self.start))
154 self._selection_changed()
155
156 def show_window(self, comp_lists, index, complete, mode, userWantsWin):
157 """Show the autocomplete list, bind events.
Terry Jan Reedy12131232019-08-04 19:48:52 -0400158
159 If complete is True, complete the text, and if there is exactly
160 one matching completion, don't open a list.
161 """
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000162 # Handle the start we already have
163 self.completions, self.morecompletions = comp_lists
164 self.mode = mode
165 self.startindex = self.widget.index(index)
166 self.start = self.widget.get(self.startindex, "insert")
167 if complete:
168 completed = self._complete_string(self.start)
Serhiy Storchakadd4754e2013-09-11 22:46:27 +0300169 start = self.start
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000170 self._change_start(completed)
171 i = self._binary_search(completed)
172 if self.completions[i] == completed and \
173 (i == len(self.completions)-1 or
174 self.completions[i+1][:len(completed)] != completed):
175 # There is exactly one matching completion
Serhiy Storchakadd4754e2013-09-11 22:46:27 +0300176 return completed == start
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000177 self.userwantswindow = userWantsWin
178 self.lasttypedstart = self.start
179
180 # Put widgets in place
181 self.autocompletewindow = acw = Toplevel(self.widget)
182 # Put it in a position so that it is not seen.
183 acw.wm_geometry("+10000+10000")
184 # Make it float
185 acw.wm_overrideredirect(1)
186 try:
187 # This command is only needed and available on Tk >= 8.4.0 for OSX
188 # Without it, call tips intrude on the typing process by grabbing
189 # the focus.
190 acw.tk.call("::tk::unsupported::MacWindowStyle", "style", acw._w,
191 "help", "noActivates")
192 except TclError:
193 pass
194 self.scrollbar = scrollbar = Scrollbar(acw, orient=VERTICAL)
195 self.listbox = listbox = Listbox(acw, yscrollcommand=scrollbar.set,
Terry Jan Reedy491ef532019-03-10 20:18:40 -0400196 exportselection=False)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000197 for item in self.completions:
198 listbox.insert(END, item)
199 self.origselforeground = listbox.cget("selectforeground")
200 self.origselbackground = listbox.cget("selectbackground")
201 scrollbar.config(command=listbox.yview)
202 scrollbar.pack(side=RIGHT, fill=Y)
203 listbox.pack(side=LEFT, fill=BOTH, expand=True)
Terry Jan Reedyd2134c72015-09-26 20:03:57 -0400204 acw.lift() # work around bug in Tk 8.5.18+ (issue #24570)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000205
206 # Initialize the listbox selection
207 self.listbox.select_set(self._binary_search(self.start))
208 self._selection_changed()
209
210 # bind events
mlouielu778b4842017-06-14 23:13:19 +0800211 self.hideaid = acw.bind(HIDE_VIRTUAL_EVENT_NAME, self.hide_event)
212 self.hidewid = self.widget.bind(HIDE_VIRTUAL_EVENT_NAME, self.hide_event)
213 acw.event_add(HIDE_VIRTUAL_EVENT_NAME, HIDE_FOCUS_OUT_SEQUENCE)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000214 for seq in HIDE_SEQUENCES:
215 self.widget.event_add(HIDE_VIRTUAL_EVENT_NAME, seq)
mlouielu778b4842017-06-14 23:13:19 +0800216
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000217 self.keypressid = self.widget.bind(KEYPRESS_VIRTUAL_EVENT_NAME,
218 self.keypress_event)
219 for seq in KEYPRESS_SEQUENCES:
220 self.widget.event_add(KEYPRESS_VIRTUAL_EVENT_NAME, seq)
221 self.keyreleaseid = self.widget.bind(KEYRELEASE_VIRTUAL_EVENT_NAME,
222 self.keyrelease_event)
223 self.widget.event_add(KEYRELEASE_VIRTUAL_EVENT_NAME,KEYRELEASE_SEQUENCE)
224 self.listupdateid = listbox.bind(LISTUPDATE_SEQUENCE,
Thomas Wouterscf297e42007-02-23 15:07:44 +0000225 self.listselect_event)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000226 self.winconfigid = acw.bind(WINCONFIG_SEQUENCE, self.winconfig_event)
227 self.doubleclickid = listbox.bind(DOUBLECLICK_SEQUENCE,
228 self.doubleclick_event)
Terry Jan Reedyc74fb9c2016-07-24 20:35:43 -0400229 return None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000230
231 def winconfig_event(self, event):
232 if not self.is_active():
233 return
234 # Position the completion list window
Thomas Wouterscf297e42007-02-23 15:07:44 +0000235 text = self.widget
236 text.see(self.startindex)
237 x, y, cx, cy = text.bbox(self.startindex)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000238 acw = self.autocompletewindow
Thomas Wouterscf297e42007-02-23 15:07:44 +0000239 acw_width, acw_height = acw.winfo_width(), acw.winfo_height()
240 text_width, text_height = text.winfo_width(), text.winfo_height()
241 new_x = text.winfo_rootx() + min(x, max(0, text_width - acw_width))
242 new_y = text.winfo_rooty() + y
243 if (text_height - (y + cy) >= acw_height # enough height below
244 or y < acw_height): # not enough height above
245 # place acw below current line
246 new_y += cy
247 else:
248 # place acw above current line
249 new_y -= acw_height
250 acw.wm_geometry("+%d+%d" % (new_x, new_y))
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000251
mlouielu778b4842017-06-14 23:13:19 +0800252 if platform.system().startswith('Windows'):
Terry Jan Reedy33c74202018-06-20 22:49:55 -0400253 # See issue 15786. When on Windows platform, Tk will misbehave
mlouielu778b4842017-06-14 23:13:19 +0800254 # to call winconfig_event multiple times, we need to prevent this,
255 # otherwise mouse button double click will not be able to used.
256 acw.unbind(WINCONFIG_SEQUENCE, self.winconfigid)
257 self.winconfigid = None
258
259 def _hide_event_check(self):
260 if not self.autocompletewindow:
261 return
262
263 try:
264 if not self.autocompletewindow.focus_get():
265 self.hide_window()
266 except KeyError:
267 # See issue 734176, when user click on menu, acw.focus_get()
268 # will get KeyError.
Terry Jan Reedyc665dfd2016-07-24 23:01:28 -0400269 self.hide_window()
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000270
mlouielu778b4842017-06-14 23:13:19 +0800271 def hide_event(self, event):
272 # Hide autocomplete list if it exists and does not have focus or
273 # mouse click on widget / text area.
274 if self.is_active():
275 if event.type == EventType.FocusOut:
Terry Jan Reedy33c74202018-06-20 22:49:55 -0400276 # On Windows platform, it will need to delay the check for
mlouielu778b4842017-06-14 23:13:19 +0800277 # acw.focus_get() when click on acw, otherwise it will return
278 # None and close the window
279 self.widget.after(1, self._hide_event_check)
280 elif event.type == EventType.ButtonPress:
281 # ButtonPress event only bind to self.widget
282 self.hide_window()
283
Thomas Wouterscf297e42007-02-23 15:07:44 +0000284 def listselect_event(self, event):
Terry Jan Reedyc74fb9c2016-07-24 20:35:43 -0400285 if self.is_active():
286 self.userwantswindow = True
287 cursel = int(self.listbox.curselection()[0])
288 self._change_start(self.completions[cursel])
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000289
290 def doubleclick_event(self, event):
291 # Put the selected completion in the text, and close the list
292 cursel = int(self.listbox.curselection()[0])
293 self._change_start(self.completions[cursel])
294 self.hide_window()
295
296 def keypress_event(self, event):
297 if not self.is_active():
Terry Jan Reedyc74fb9c2016-07-24 20:35:43 -0400298 return None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000299 keysym = event.keysym
300 if hasattr(event, "mc_state"):
301 state = event.mc_state
302 else:
303 state = 0
Thomas Wouterscf297e42007-02-23 15:07:44 +0000304 if keysym != "Tab":
305 self.lastkey_was_tab = False
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000306 if (len(keysym) == 1 or keysym in ("underscore", "BackSpace")
Terry Jan Reedy12131232019-08-04 19:48:52 -0400307 or (self.mode == FILES and keysym in
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000308 ("period", "minus"))) \
309 and not (state & ~MC_SHIFT):
310 # Normal editing of text
311 if len(keysym) == 1:
312 self._change_start(self.start + keysym)
313 elif keysym == "underscore":
314 self._change_start(self.start + '_')
315 elif keysym == "period":
316 self._change_start(self.start + '.')
317 elif keysym == "minus":
318 self._change_start(self.start + '-')
319 else:
320 # keysym == "BackSpace"
321 if len(self.start) == 0:
322 self.hide_window()
Terry Jan Reedyc74fb9c2016-07-24 20:35:43 -0400323 return None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000324 self._change_start(self.start[:-1])
325 self.lasttypedstart = self.start
326 self.listbox.select_clear(0, int(self.listbox.curselection()[0]))
327 self.listbox.select_set(self._binary_search(self.start))
328 self._selection_changed()
329 return "break"
330
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000331 elif keysym == "Return":
terryjreedy32fd8742017-06-14 15:43:15 -0400332 self.complete()
Guido van Rossum8ce8a782007-11-01 19:42:39 +0000333 self.hide_window()
terryjreedy32fd8742017-06-14 15:43:15 -0400334 return 'break'
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000335
Terry Jan Reedy12131232019-08-04 19:48:52 -0400336 elif (self.mode == ATTRS and keysym in
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000337 ("period", "space", "parenleft", "parenright", "bracketleft",
338 "bracketright")) or \
Terry Jan Reedy12131232019-08-04 19:48:52 -0400339 (self.mode == FILES and keysym in
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000340 ("slash", "backslash", "quotedbl", "apostrophe")) \
341 and not (state & ~MC_SHIFT):
342 # If start is a prefix of the selection, but is not '' when
343 # completing file names, put the whole
344 # selected completion. Anyway, close the list.
345 cursel = int(self.listbox.curselection()[0])
346 if self.completions[cursel][:len(self.start)] == self.start \
Terry Jan Reedy12131232019-08-04 19:48:52 -0400347 and (self.mode == ATTRS or self.start):
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000348 self._change_start(self.completions[cursel])
349 self.hide_window()
Terry Jan Reedyc74fb9c2016-07-24 20:35:43 -0400350 return None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000351
352 elif keysym in ("Home", "End", "Prior", "Next", "Up", "Down") and \
353 not state:
354 # Move the selection in the listbox
355 self.userwantswindow = True
356 cursel = int(self.listbox.curselection()[0])
357 if keysym == "Home":
358 newsel = 0
359 elif keysym == "End":
360 newsel = len(self.completions)-1
361 elif keysym in ("Prior", "Next"):
362 jump = self.listbox.nearest(self.listbox.winfo_height()) - \
363 self.listbox.nearest(0)
364 if keysym == "Prior":
365 newsel = max(0, cursel-jump)
366 else:
367 assert keysym == "Next"
368 newsel = min(len(self.completions)-1, cursel+jump)
369 elif keysym == "Up":
370 newsel = max(0, cursel-1)
371 else:
372 assert keysym == "Down"
373 newsel = min(len(self.completions)-1, cursel+1)
374 self.listbox.select_clear(cursel)
375 self.listbox.select_set(newsel)
376 self._selection_changed()
Thomas Wouterscf297e42007-02-23 15:07:44 +0000377 self._change_start(self.completions[newsel])
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000378 return "break"
379
380 elif (keysym == "Tab" and not state):
Thomas Wouterscf297e42007-02-23 15:07:44 +0000381 if self.lastkey_was_tab:
382 # two tabs in a row; insert current selection and close acw
383 cursel = int(self.listbox.curselection()[0])
384 self._change_start(self.completions[cursel])
385 self.hide_window()
386 return "break"
387 else:
388 # first tab; let AutoComplete handle the completion
389 self.userwantswindow = True
390 self.lastkey_was_tab = True
Terry Jan Reedyc74fb9c2016-07-24 20:35:43 -0400391 return None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000392
Guido van Rossum89da5d72006-08-22 00:21:25 +0000393 elif any(s in keysym for s in ("Shift", "Control", "Alt",
394 "Meta", "Command", "Option")):
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000395 # A modifier key, so ignore
Terry Jan Reedyc74fb9c2016-07-24 20:35:43 -0400396 return None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000397
Martin v. Löwis97aa21b2012-06-03 12:26:09 +0200398 elif event.char and event.char >= ' ':
Martin v. Löwis862d13a2012-06-03 11:55:32 +0200399 # Regular character with a non-length-1 keycode
400 self._change_start(self.start + event.char)
401 self.lasttypedstart = self.start
402 self.listbox.select_clear(0, int(self.listbox.curselection()[0]))
403 self.listbox.select_set(self._binary_search(self.start))
404 self._selection_changed()
405 return "break"
406
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000407 else:
408 # Unknown event, close the window and let it through.
409 self.hide_window()
Terry Jan Reedyc74fb9c2016-07-24 20:35:43 -0400410 return None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000411
412 def keyrelease_event(self, event):
413 if not self.is_active():
414 return
415 if self.widget.index("insert") != \
416 self.widget.index("%s+%dc" % (self.startindex, len(self.start))):
417 # If we didn't catch an event which moved the insert, close window
418 self.hide_window()
419
420 def is_active(self):
421 return self.autocompletewindow is not None
422
423 def complete(self):
424 self._change_start(self._complete_string(self.start))
425 # The selection doesn't change.
426
427 def hide_window(self):
428 if not self.is_active():
429 return
430
431 # unbind events
mlouielu778b4842017-06-14 23:13:19 +0800432 self.autocompletewindow.event_delete(HIDE_VIRTUAL_EVENT_NAME,
433 HIDE_FOCUS_OUT_SEQUENCE)
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000434 for seq in HIDE_SEQUENCES:
435 self.widget.event_delete(HIDE_VIRTUAL_EVENT_NAME, seq)
mlouielu778b4842017-06-14 23:13:19 +0800436
437 self.autocompletewindow.unbind(HIDE_VIRTUAL_EVENT_NAME, self.hideaid)
438 self.widget.unbind(HIDE_VIRTUAL_EVENT_NAME, self.hidewid)
439 self.hideaid = None
440 self.hidewid = None
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000441 for seq in KEYPRESS_SEQUENCES:
442 self.widget.event_delete(KEYPRESS_VIRTUAL_EVENT_NAME, seq)
443 self.widget.unbind(KEYPRESS_VIRTUAL_EVENT_NAME, self.keypressid)
444 self.keypressid = None
445 self.widget.event_delete(KEYRELEASE_VIRTUAL_EVENT_NAME,
446 KEYRELEASE_SEQUENCE)
447 self.widget.unbind(KEYRELEASE_VIRTUAL_EVENT_NAME, self.keyreleaseid)
448 self.keyreleaseid = None
449 self.listbox.unbind(LISTUPDATE_SEQUENCE, self.listupdateid)
450 self.listupdateid = None
mlouielu778b4842017-06-14 23:13:19 +0800451 if self.winconfigid:
452 self.autocompletewindow.unbind(WINCONFIG_SEQUENCE, self.winconfigid)
453 self.winconfigid = None
454
455 # Re-focusOn frame.text (See issue #15786)
456 self.widget.focus_set()
Kurt B. Kaiserb1754452005-11-18 22:05:48 +0000457
458 # destroy widgets
459 self.scrollbar.destroy()
460 self.scrollbar = None
461 self.listbox.destroy()
462 self.listbox = None
463 self.autocompletewindow.destroy()
464 self.autocompletewindow = None
Terry Jan Reedyee5ef302018-06-15 18:20:55 -0400465
466
467if __name__ == '__main__':
468 from unittest import main
469 main('idlelib.idle_test.test_autocomplete_w', verbosity=2, exit=False)
470
471# TODO: autocomplete/w htest here