Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 17 additions & 0 deletions Lib/test/test_tkinter/test_simpledialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,23 @@ def test_go(self):
d.root.after(1, lambda: d._buttons[0].invoke())
self.assertEqual(d.go(), 0)

def test_go_foreign_grab(self):
# gh-157676: the grab and the focus can be in a window which was not
# created by tkinter, such as a native message box; they must be
# restored after the dialog.
tk = self.root.tk
tk.call('toplevel', '.foreign')
self.addCleanup(tk.call, 'destroy', '.foreign')
tk.call('wm', 'deiconify', '.foreign')
tk.call('update')
tk.call('grab', 'set', '.foreign')
tk.call('focus', '-force', '.foreign')
d = self.create()
d.root.after(1, lambda: d._buttons[0].invoke())
self.assertEqual(d.go(), 0)
self.assertEqual(tk.call('grab', 'current', self.root._w), '.foreign')
self.assertEqual(tk.call('focus'), '.foreign')


class DialogTest(AbstractDialogTest, unittest.TestCase):
# Dialog's button box is modelled on tk::MessageBox.
Expand Down
25 changes: 13 additions & 12 deletions Lib/tkinter/simpledialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,12 +722,13 @@ def askstring(title, prompt, **kw):

@contextlib.contextmanager
def _temp_grab_focus(grab, focus=None, destroy=True):
old_focus = grab.focus_get()
old_grab = grab.grab_current()
if old_grab is not None and old_grab.winfo_exists():
old_status = old_grab.grab_status()
else:
old_status = None
# Use Tcl window names rather than widgets, because the focus and the
# grab can be in a window which was not created by tkinter, such as
# a native message box.
tk = grab.tk
old_focus = tk.call('focus')
old_grab = tk.call('grab', 'current', grab._w)
old_status = tk.call('grab', 'status', old_grab) if old_grab else None
# The "grab" command will fail if another application
# already holds the grab. So catch it.
try:
Expand All @@ -741,9 +742,9 @@ def _temp_grab_focus(grab, focus=None, destroy=True):
yield

finally:
if old_focus is not None:
if old_focus and grab.getboolean(tk.call('winfo', 'exists', old_focus)):
try:
old_focus.focus_set()
tk.call('focus', old_focus)
except TclError:
pass
try:
Expand All @@ -755,15 +756,15 @@ def _temp_grab_focus(grab, focus=None, destroy=True):
grab.destroy()
except TclError:
pass
if (old_grab is not None and old_grab.winfo_exists()
and old_grab.winfo_ismapped()):
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.
try:
if old_status == 'global':
old_grab.grab_set_global()
tk.call('grab', 'set', '-global', old_grab)
else:
old_grab.grab_set()
tk.call('grab', 'set', old_grab)
except TclError:
pass

Expand Down
Loading