Uh oh!
There was an error while loading. Please reload this page.
gh-154357: Hide the root window in the tkinter dialog tests - #154370
gh-154357: Hide the root window in the tkinter dialog tests#154370serhiy-storchaka wants to merge 2 commits into
Conversation
Tk delivers generated keyboard events to the window which has the focus. While the root window is visible, the window manager can take the focus back from the dialog, and the events are dropped. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
ByteFlowing1337
commented
Jul 21, 2026
This fixes the reported issue, but there are still some flaky tests under ======================================================================FAIL: test_wm_stackorder (test.test_tkinter.test_misc.WmTest.test_wm_stackorder)
----------------------------------------------------------------------Traceback (mostrecentcalllast):
File"/home/a/cpython/Lib/test/test_tkinter/test_misc.py", line1335, intest_wm_stackorderself.assertGreater(names.index(str(t1)), names.index(str(t2)))
~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^AssertionError: 1notgreaterthan2============================================================================================================================================FAIL: test_focus (test.test_tkinter.test_misc.EventTest.test_focus)
----------------------------------------------------------------------Traceback (mostrecentcalllast):
File"/home/a/cpython/Lib/test/test_tkinter/test_misc.py", line1364, intest_focusself.assertEqual(len(events), 1, events)
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^AssertionError: 2!=1 : [<FocusInevent>, <FocusInevent>]
====================================================================== |
The window manager can reorder the toplevel windows while they are still being mapped, and can take the focus back while events are processed. Wait until the windows are mapped before changing the stacking order, and force the focus right before generating a keyboard event. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
serhiy-storchaka
commented
Jul 21, 2026
I cannot reproduce the duplicate |
ByteFlowing1337
commented
Jul 21, 2026
I tested on WSL2, so my window manager environment may not be standard. It's Weston. In I applied your patch for fixing ======================================================================FAIL: test_wm_stackorder (test.test_tkinter.test_misc.WmTest.test_wm_stackorder)
----------------------------------------------------------------------Traceback (mostrecentcalllast):
File"/home/a/cpython/Lib/test/test_tkinter/test_misc.py", line1336, intest_wm_stackorderself.assertGreater(names.index(str(t1)), names.index(str(t2)))
~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^AssertionError: 0notgreaterthan2----------------------------------------------------------------------If I remove that assertion, the following assertions can still fail: self.assertIs(t1.wm_stackorder('isabove', t2), True)
self.assertIs(t1.wm_stackorder('isbelow', t2), False)
self.assertIs(t2.wm_stackorder('isbelow', t1), True)And ======================================================================FAIL: test_focus_methods (test.test_tkinter.test_misc.MiscTest.test_focus_methods)
----------------------------------------------------------------------Traceback (mostrecentcalllast):
File"/home/a/cpython/Lib/test/test_tkinter/test_misc.py", line519, intest_focus_methodsself.assertIs(self.root.focus_get(), b)
~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^AssertionError: Noneisnot<tkinter.Buttonobject .+frame.+button>---------------------------------------------------------------------- |
The tkinter dialog tests drive the dialogs with generated keyboard events. Tk delivers such an event to the window which has the keyboard focus, not to the window passed to
event_generate(), and drops it if the focus is elsewhere. The binding then never fires, and the test fails with an unset result.AbstractTkTest.setUp()deiconifies the root window, and the window manager can take the focus back from the dialog. Waiting until the dialog has the focus is not enough, because it is lost again a moment later. Withdraw the root window instead, so that nothing competes for the focus; the dialogs are toplevels of their own and do not need a visible root.This fixes 9 failing tests in
test_simpledialogand a flaky one intest_filedialogon X11 with KWin. The failure does not reproduce without a window manager, which is why the buildbots do not show it.test_boundary_values_acceptedfails #154357