Skip to content

Commit cf9f958

Browse files
[3.15] gh-162650: Fix tkinter calls from other threads after a nested mainloop() (GH-162651) (GH-162673)
A nested mainloop() now restores the dispatching flag instead of resetting it, so the outer mainloop() still accepts calls from other threads. (cherry picked from commit b50b43d) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9ad66bb commit cf9f958

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

‎Lib/test/test_tkinter/test_misc.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import signal
66
import sys
77
import textwrap
8+
import threading
89
import unittest
910
import weakref
1011
import tkinter
@@ -597,6 +598,31 @@ def callback(event):
597598
user32.SendMessageW(hwnd, WM_EXITSIZEMOVE, 0, 0)
598599
self.assertTrue(sizes)
599600

601+
def test_nested_mainloop(self):
602+
# gh-162650: Calls from other thread are still dispatched after
603+
# a nested mainloop() exits.
604+
root = self.root
605+
results = []
606+
def call():
607+
try:
608+
results.append(root.tk.call('expr', '6*7'))
609+
except RuntimeError as e:
610+
results.append(e)
611+
def start():
612+
root.after(0, root.quit)
613+
root.mainloop() # nested
614+
thread = threading.Thread(target=call)
615+
thread.start()
616+
def wait():
617+
if thread.is_alive():
618+
root.after(10, wait)
619+
else:
620+
root.quit()
621+
wait()
622+
root.after(0, start)
623+
root.mainloop()
624+
self.assertEqual(results, [42])
625+
600626
def test_wait_variable(self):
601627
var = tkinter.StringVar(self.root)
602628
self.assertEqual(self.root.waitvar, self.root.wait_variable)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix :mod:`tkinter` calls from other threads failing with :exc:`RuntimeError`
2+
after a nested :meth:`!mainloop` exited, for example after
3+
:meth:`!SimpleDialog.go` returned.

‎Modules/_tkinter.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3005,6 +3005,9 @@ _tkinter_tkapp_mainloop_impl(TkappObject *self, int threshold)
30053005
PyThreadState *tstate = PyThreadState_Get();
30063006

30073007
CHECK_TCL_APPARTMENT(self);
3008+
/* mainloop() can be nested, the outer mainloop() still dispatches
3009+
events after the inner one exits. */
3010+
int dispatching = self->dispatching;
30083011
self->dispatching = 1;
30093012

30103013
quitMainLoop = 0;
@@ -3033,13 +3036,13 @@ _tkinter_tkapp_mainloop_impl(TkappObject *self, int threshold)
30333036
}
30343037

30353038
if (PyErr_CheckSignals() != 0) {
3036-
self->dispatching = 0;
3039+
self->dispatching = dispatching;
30373040
return NULL;
30383041
}
30393042
if (result < 0)
30403043
break;
30413044
}
3042-
self->dispatching = 0;
3045+
self->dispatching = dispatching;
30433046
quitMainLoop = 0;
30443047

30453048
if (errorInCmd) {

0 commit comments

Comments
 (0)