Skip to content

Commit a89df13

Browse files
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".
1 parent 7560111 commit a89df13

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

‎Lib/tkinter/simpledialog.py‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,9 @@ def _temp_grab_focus(grab, focus=None, destroy=True):
742742
yield
743743

744744
finally:
745-
if old_focus and grab.getboolean(tk.call('winfo', 'exists', old_focus)):
745+
# The old windows and even the whole application can be destroyed
746+
# by now, so catch errors.
747+
if old_focus:
746748
try:
747749
tk.call('focus', old_focus)
748750
except TclError:
@@ -756,15 +758,17 @@ def _temp_grab_focus(grab, focus=None, destroy=True):
756758
grab.destroy()
757759
except TclError:
758760
pass
759-
if (old_grab and grab.getboolean(tk.call('winfo', 'exists', old_grab))
760-
and grab.getboolean(tk.call('winfo', 'ismapped', old_grab))):
761-
# The "grab" command will fail if another application
762-
# already holds the grab. So catch it.
761+
if old_grab:
762+
# Do not restore the grab if the window is no longer mapped:
763+
# a local grab on an unmapped window would block the application.
764+
# The "grab" command will fail if another application already
765+
# holds the grab on a window with the same name. So catch it.
763766
try:
764-
if old_status == 'global':
765-
tk.call('grab', 'set', '-global', old_grab)
766-
else:
767-
tk.call('grab', 'set', old_grab)
767+
if grab.getboolean(tk.call('winfo', 'ismapped', old_grab)):
768+
if old_status == 'global':
769+
tk.call('grab', 'set', '-global', old_grab)
770+
else:
771+
tk.call('grab', 'set', old_grab)
768772
except TclError:
769773
pass
770774

0 commit comments

Comments
 (0)