@@ -242,37 +242,46 @@ def winconfig_event(self, event):
242242 self .is_configuring = True
243243 if not self .is_active ():
244244 return
245- # Position the completion list window
246- text = self .widget
247- text .see (self .startindex )
248- x , y , cx , cy = text .bbox (self .startindex )
249- acw = self .autocompletewindow
250- if platform .system ().startswith ('Windows' ):
251- # On Windows an update() call is needed for the completion list
252- # window to be created, so that we can fetch its width and
253- # height. However, this is not needed on other platforms (tested
254- # on Ubuntu and macOS) but at one point began causing freezes on
255- # macOS. See issues 37849 and 41611.
256- acw .update ()
257- acw_width , acw_height = acw .winfo_width (), acw .winfo_height ()
258- text_width , text_height = text .winfo_width (), text .winfo_height ()
259- new_x = text .winfo_rootx () + min (x , max (0 , text_width - acw_width ))
260- new_y = text .winfo_rooty () + y
261- if (text_height - (y + cy ) >= acw_height # enough height below
262- or y < acw_height ): # not enough height above
263- # place acw below current line
264- new_y += cy
265- else :
266- # place acw above current line
267- new_y -= acw_height
268- acw .wm_geometry ("+%d+%d" % (new_x , new_y ))
269- acw .update_idletasks ()
245+
246+ # Since the <Configure> event may occur after the completion window is gone,
247+ # catch potential TclError exceptions when accessing acw. See: bpo-41611.
248+ try :
249+ # Position the completion list window
250+ text = self .widget
251+ text .see (self .startindex )
252+ x , y , cx , cy = text .bbox (self .startindex )
253+ acw = self .autocompletewindow
254+ if platform .system ().startswith ('Windows' ):
255+ # On Windows an update() call is needed for the completion
256+ # list window to be created, so that we can fetch its width
257+ # and height. However, this is not needed on other platforms
258+ # (tested on Ubuntu and macOS) but at one point began
259+ # causing freezes on macOS. See issues 37849 and 41611.
260+ acw .update ()
261+ acw_width , acw_height = acw .winfo_width (), acw .winfo_height ()
262+ text_width , text_height = text .winfo_width (), text .winfo_height ()
263+ new_x = text .winfo_rootx () + min (x , max (0 , text_width - acw_width ))
264+ new_y = text .winfo_rooty () + y
265+ if (text_height - (y + cy ) >= acw_height # enough height below
266+ or y < acw_height ): # not enough height above
267+ # place acw below current line
268+ new_y += cy
269+ else :
270+ # place acw above current line
271+ new_y -= acw_height
272+ acw .wm_geometry ("+%d+%d" % (new_x , new_y ))
273+ acw .update_idletasks ()
274+ except TclError :
275+ pass
270276
271277 if platform .system ().startswith ('Windows' ):
272- # See issue 15786. When on Windows platform, Tk will misbehave
278+ # See issue 15786. When on Windows platform, Tk will misbehave
273279 # to call winconfig_event multiple times, we need to prevent this,
274280 # otherwise mouse button double click will not be able to used.
275- acw .unbind (WINCONFIG_SEQUENCE , self .winconfigid )
281+ try :
282+ acw .unbind (WINCONFIG_SEQUENCE , self .winconfigid )
283+ except TclError :
284+ pass
276285 self .winconfigid = None
277286
278287 self .is_configuring = False
0 commit comments