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>
|
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 (most recent call last):
File "/home/a/cpython/Lib/test/test_tkinter/test_misc.py", line 1335, in test_wm_stackorder
self.assertGreater(names.index(str(t1)), names.index(str(t2)))
~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: 1 not greater than 2
============================================================================================================================================
FAIL: test_focus (test.test_tkinter.test_misc.EventTest.test_focus)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/a/cpython/Lib/test/test_tkinter/test_misc.py", line 1364, in test_focus
self.assertEqual(len(events), 1, events)
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: 2 != 1 : [<FocusIn event>, <FocusIn event>]
====================================================================== |
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>
|
I cannot reproduce the duplicate |
|
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 (most recent call last):
File "/home/a/cpython/Lib/test/test_tkinter/test_misc.py", line 1336, in test_wm_stackorder
self.assertGreater(names.index(str(t1)), names.index(str(t2)))
~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: 0 not greater than 2
----------------------------------------------------------------------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 (most recent call last):
File "/home/a/cpython/Lib/test/test_tkinter/test_misc.py", line 519, in test_focus_methods
self.assertIs(self.root.focus_get(), b)
~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: None is not <tkinter.Button object .+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