Skip to content

Commit f10d09d

Browse files
[3.14] gh-162650: Fix tkinter calls from other threads after a nested mainloop() (GH-162651) (GH-162674)
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 73da836 commit f10d09d

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
@@ -3,6 +3,7 @@
33
import signal
44
import sys
55
import textwrap
6+
import threading
67
import unittest
78
import weakref
89
import tkinter
@@ -595,6 +596,31 @@ def callback(event):
595596
user32.SendMessageW(hwnd, WM_EXITSIZEMOVE, 0, 0)
596597
self.assertTrue(sizes)
597598

599+
def test_nested_mainloop(self):
600+
# gh-162650: Calls from other thread are still dispatched after
601+
# a nested mainloop() exits.
602+
root = self.root
603+
results = []
604+
def call():
605+
try:
606+
results.append(root.tk.call('expr', '6*7'))
607+
except RuntimeError as e:
608+
results.append(e)
609+
def start():
610+
root.after(0, root.quit)
611+
root.mainloop() # nested
612+
thread = threading.Thread(target=call)
613+
thread.start()
614+
def wait():
615+
if thread.is_alive():
616+
root.after(10, wait)
617+
else:
618+
root.quit()
619+
wait()
620+
root.after(0, start)
621+
root.mainloop()
622+
self.assertEqual(results, [42])
623+
598624
def test_wait_variable(self):
599625
var = tkinter.StringVar(self.root)
600626
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
@@ -3000,6 +3000,9 @@ _tkinter_tkapp_mainloop_impl(TkappObject *self, int threshold)
30003000
PyThreadState *tstate = PyThreadState_Get();
30013001

30023002
CHECK_TCL_APPARTMENT(self);
3003+
/* mainloop() can be nested, the outer mainloop() still dispatches
3004+
events after the inner one exits. */
3005+
int dispatching = self->dispatching;
30033006
self->dispatching = 1;
30043007

30053008
quitMainLoop = 0;
@@ -3028,13 +3031,13 @@ _tkinter_tkapp_mainloop_impl(TkappObject *self, int threshold)
30283031
}
30293032

30303033
if (PyErr_CheckSignals() != 0) {
3031-
self->dispatching = 0;
3034+
self->dispatching = dispatching;
30323035
return NULL;
30333036
}
30343037
if (result < 0)
30353038
break;
30363039
}
3037-
self->dispatching = 0;
3040+
self->dispatching = dispatching;
30383041
quitMainLoop = 0;
30393042

30403043
if (errorInCmd) {

0 commit comments

Comments
 (0)