Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Catch errors when restoring the focus and the grab
The dialog can destroy a temporary default root, and then even "winfo"
fails with "application has been destroyed".
  • Loading branch information
serhiy-storchaka committed Sep 21, 2026
commit a89df13e667db3de553f2a0f0fb0ea216d4e02ee
22 changes: 13 additions & 9 deletions Lib/tkinter/simpledialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,9 @@ def _temp_grab_focus(grab, focus=None, destroy=True):
yield

finally:
if old_focus and grab.getboolean(tk.call('winfo', 'exists', old_focus)):
# The old windows and even the whole application can be destroyed
# by now, so catch errors.
if old_focus:
try:
tk.call('focus', old_focus)
except TclError:
Expand All @@ -756,15 +758,17 @@ def _temp_grab_focus(grab, focus=None, destroy=True):
grab.destroy()
except TclError:
pass
if (old_grab and grab.getboolean(tk.call('winfo', 'exists', old_grab))
and grab.getboolean(tk.call('winfo', 'ismapped', old_grab))):
# The "grab" command will fail if another application
# already holds the grab. So catch it.
if old_grab:
# Do not restore the grab if the window is no longer mapped:
# a local grab on an unmapped window would block the application.
# The "grab" command will fail if another application already
# holds the grab on a window with the same name. So catch it.
try:
if old_status == 'global':
tk.call('grab', 'set', '-global', old_grab)
else:
tk.call('grab', 'set', old_grab)
if grab.getboolean(tk.call('winfo', 'ismapped', old_grab)):
if old_status == 'global':
tk.call('grab', 'set', '-global', old_grab)
else:
tk.call('grab', 'set', old_grab)
except TclError:
pass

Expand Down
Loading