Tk Source Code

View Ticket
Login
Ticket UUID: 88ffa704538394b344ba503d937c2c0fb699a652
Title: Above roughly 10000 widgets wish hangs on Windows
Type: Bug Version: 8.6.7
Submitter: SoleSoul Created on: 2023-05-11 08:55:35
Subsystem: 68. Win Window Operations Assigned To: nobody
Priority: 5 Medium Severity: Important
Status: Open Last Modified: 2026-11-12 10:46:54
Resolution: None Closed By: nobody
    Closed on: 2026-10-20 12:47:56
Description:
We have a commercial data heavy application which sometimes needs to have more than 10000 widgets at the same time.
It works on Linux but hangs on Windows despite the running computer having plenty of spare resources.

Attached is a short TCL code to reproduce the issue. To be run with wish on Windows.
I know very little TCL and I usually use Tk trough Python.
Here is the Python bug report which was closed because the issue originates from Tk itself: https://bugs.python.org/issue41632
User Comments: nemethi (claiming to be Csaba Nemethi) added on 2026-11-12 10:46:54:

Reverted the commits [92ccdec3] and [3d4a73e5].


jan.nijtmans added on 2026-11-12 10:38:51:

Thanks!


nemethi (claiming to be Csaba Nemethi) added on 2026-11-12 09:28:52:

Jan, many thanks for reverting the commit [3efcdcac]! I will revert [92ccdec3] and [3d4a73e5] ASAP (within the next few hours).


jan.nijtmans added on 2026-11-11 20:50:43:

I agree with this. So I did a quick revert on core-9-0-branch, this one has the highest priority.


serhiy.storchaka added on 2026-11-11 20:07:58:

Csaba, unless you have another opinion, could you please revert [92ccdec3], [3efcdcac] and [3d4a73e5] (fix 1)?


marc_culler (claiming to be Marc Culler) added on 2026-11-11 15:51:02:
Erik, another thing to be careful about is that there are several
independent manifestions of "mapping a window":

* When its TK_MAPPED flag is set;
* When it receives a <Map> event;
* When it appears on the screen;

and maybe

* When it receives a <VisibilityNotify> event.

marc_culler (claiming to be Marc Culler) added on 2026-11-11 15:45:12:
I am still inclined towards the simple fix 1.  This issue seems to have
unexpectedly deep interactions, and I think we would benefit from
having some time to think through possible reorganizations of Tk's logic
for 9.2.  I have in mind things like changing how Windows computes the
window Id, and thinking about what the purpose of wm withdraw actually is,
and whether it should even exist at all. Also, perhaps classic widgets
should behave more like Ttk widgets.

serhiy.storchaka added on 2026-11-11 14:21:32:

Thank you, Marc. You are right that fix 2 does not fix the Python reproducer: it checks that events generated with event generate for a ttk widget in a hidden window are delivered. But classic widgets never received such events: a widget gets its window only when it is first mapped. ttk widgets received them only because the ttk constructor created the window early.

The theme itself is already handled on main and core-9-0-branch, where WidgetWorldChanged() updates the layout on the first map. Only 8.6 lacks this, so fix 2 is needed only there.

But there is another regression: the virtual events that ttk widgets send themselves are lost if the widget was never mapped. For example, $notebook select in a hidden window does not send <<NotebookTabChanged>>, and $tree selection set does not send <<TreeviewSelect>>, even after the window is shown. The classic text widget creates its window before sending <<Modified>> and <<Selection>>, so these are delivered. On main this also matters for accessibility, which binds <<NotebookTabChanged>> and <<TreeviewSelect>>.

So another option:

  1. Create the window before a ttk widget sends its own virtual event (notebook, treeview, panedwindow), as text does, on all branches, plus fix 2 on 8.6. This keeps the Windows fix for the other ttk widgets. Events generated with event generate for widgets that were never mapped stay undelivered, as for classic widgets.

Fix 1 is simpler and restores the old behavior completely; fix 4 makes ttk widgets behave like classic ones. Which would you prefer?


marc_culler (claiming to be Marc Culler) added on 2026-11-11 13:55:09:
I think I would vote for fix 1. Fix 2 doesn't fix the python issue.  I
suspect that fix 3 would produce artifacts and it might be problematic for
9.0 and 8.6.  (It might be OK if the new field was at the end, but why
risk it.)

Fix 1 does not fix the 10000 widget barrier.  But, to be honest, creating
more than 10000 widgets seems like a design flaw.  Surely it would be
possible to create 10000 objects of a different class, which manages
a widget, and doesn't actually create its widget until it is needed.
Or, it might work to use canvas text items instead of labels - there is
no limit on the number of those.  If you read the online discussion of
the 10000 window limit you will find pretty wide support for the notion
that no app should need 10000 windows on a system which only allows a
combined total of 32000 for all processes.  And making it possible
to have more than 10000 labels, but only under the condition that fewer
than 10000 of them are mapped at any time, seems like a half measure
that will be difficult for an app with 10000 labels to comply with.

Maybe, for 9.1 or 9.2, someone with Claude by their side could consider
reworking the Windows port to make it use the address of the XWindow as
the window ID instead of the hwnd. (Or something along those lines.)

erikleunissen added on 2026-11-11 10:45:20:
Perhaps irrelevant for this particular case. Nevertheless: beware!

Regarding:

    "... so, like a classic widget, it does not receive events generated
     for it before that ... "

<Configure> events may be serviced before a newly created window
is first mapped. As witnessed by (on Linux):

% package require Tk
9.1.0
% bind all <Map> {puts "%W <Map> [winfo ismapped %W]"}
% bind all <Configure> {puts "%W <Configure> [winfo ismapped %W]"}
% toplevel .t
.t
.t <Configure> 0
.t <Map> 1
.t <Configure> 1

serhiy.storchaka added on 2026-11-11 08:21:44:

Thank you, Marc. I know about this issue and was hesitating between possible fixes.

What changed: a ttk widget no longer gets an X window until it is first mapped, so, like a classic widget, it does not receive events generated for it before that. For ordinary events (<Enter> etc.) this is how classic widgets always behaved. The real regression is the theme: <<ThemeChanged>> is not delivered to unmapped ttk widgets. On the first map the widget catches up and redisplays, but it does not rebuild its layout as the <<ThemeChanged>> handler does, so it can keep the layout of the old theme.

Deferring only the window handle on Windows is not simple: the window ID, which event delivery needs, is the HWND wrapper there, and it is used in many places.

Possible fixes:

  1. Restore Tk_MakeWindowExist() in the ttk widget constructor, at least on core-8-6-branch and core-9-0-branch. This is the old behavior; the Windows limit of about 10000 widgets comes back.
  2. On the first map, also rebuild the layout (UpdateLayout()). One line, keeps the Windows fix. But application bindings on <<ThemeChanged>> still do not fire for a widget that was never mapped.
  3. On the first map, generate <<ThemeChanged>> if the theme was changed since the widget was created. This also makes application bindings fire, but it needs a new field in WidgetCore, so it is for trunk only.

For 9.0.5 and 8.6 I lean to 1 or 2. Which would you prefer?


marc_culler (claiming to be Marc Culler) added on 2026-11-11 04:34:10:
It seems that the Windows port creates a window handle in Tk_MakeWindow,
but the macOS port does not do the equivalent thing.  Instead the macOS
port creates the TKWindow* for a toplevel when the toplevel is first mapped.

Of course, toplevels are not the issue here.  The issue is that every widget
has a window handle.  In macOS only a toplevel window has a pointer to
a member of the native window class TKWindow (derived from NSWindow).
Still, I think this supports deferring creation of a window handle until
the time when a widget is first mapped, but not deferring the creation of
a TkWindow for the widget.

marc_culler (claiming to be Marc Culler) added on 2026-11-11 00:38:00:
Unfortunately, I have to reopen this ticket.  This ticket was addressing a
Windows problem that only arose if more than 10000 widgets are created.
But the fix affected all platforms even when there are only a normal number
of windows.  The side effects of the fix are now appearing as issues with
the Python Tkinter module.

The problem reported at https://github.com/python/cpython/issues/161428 is
that if you withddraw a window, change the ttk theme, and then deiconify
the window its theme is not changed.  Before the fix, it would have changed.

The reason for this is that the commits commits [92ccdec3], [3efcdcac],
and [3d4a73e5] removed a call to Tk_MakeWindowExist(tkwin) in
TtkWidgetConstructorObjCmd and instead deferred creation of the widget
until it was actually mapped.  The justification given in the related ticket
[31085d2747] was: "ttk widgets now behave like classic widgets, which also
makes their creation on Windows about 10 times faster."

However, it turns out that there was a reason why unmapped ttk widgets
need to receive events which does not exist for classic widgets.  The ttk
widgets need to know what their theme is, even if they are withdrawn and
hence unmapped.

The underlying Windows problem was that there is a limit to how many
window handles a process can create, and a side effect of Tk_MakeWindowExist
was to create a window handle.

I think that a better fix for this would be to defer creation of a window
handle until the window gets mapped, but not defer creation of the TkWindow
struct.  I think that would allow unmapped ttk widgets to receive virtual
events, like <<ThemeChanged>>, without forcing a window which is never
mapped to waste a precious window handle.

This would be a windows-only solution to a windows-only problem, which seems
like a better approach to me.

nemethi (claiming to be Csaba Nemethi) added on 2026-10-20 12:47:56:

Serhiy, many thanks for the fix! Merged into main, core-9-0-branch, and core-8-6-branch by commits [92ccdec3], [3efcdcac], and [3d4a73e5].


serhiy.storchaka added on 2026-09-18 13:30:20:

See [31085d2747]: the per-process limit of 10000 window handles on Windows; [7fc6686e76] avoids it for ttk widgets that are never mapped.


SoleSoul added on 2023-05-14 06:53:13:
I confirm that if I open wish and paste the code inside I get the same behavior as you see.
The test I did was to run 'wish ten_k.tcl' from the command line. The window seems to never show up.

oehhar added on 2023-05-11 12:49:51:

Hi lahav, thank you for the bug report.

For me, your example script does not hang. But wish is totally slow and the final gridded label is located outside the wish window. It is in the upper-left corner.

My platform is:

  • MS-Windows 10 64 bit
  • tcl/tk 8.6.13 32 bit self compiled.

What I did:

  • start wish86.exe
  • paste the content of your script into the console.

Is this also what you are seeing?

Take care, Harald


Attachments: