@@ -97,6 +97,49 @@ Or, more often::
97
97
98
98
from tkinter import *
99
99
100
+
101
+ Threading model
102
+ ---------------
103
+
104
+ A Tcl interpreter has one stream of execution and, unlike most other GUI
105
+ toolkits, doesn't provide a blocking event loop.
106
+ Instead, Tcl code is supposed to pump the event queue regularly at strategic
107
+ moments -- after an action that triggers an event, before a later action that
108
+ needs the result of that event. As such, all Tcl commands are designed to work
109
+ without an event loop running, only event handlers will not be executed
110
+ until the queue is processed. This allows to work completely in one thread,
111
+ i.e. even in environments that have no thread support at all.
112
+
113
+ This is in stark constrast to the execution model for most other GUI toolkits:
114
+ a dedicated OS thread (usually called the "UI thread") runs the GUI event loop
115
+ constantly, and other threads send work items to it -- either complete with
116
+ payload reference, or as agreed-upon "events" which result in it executing a
117
+ predefined "event handler".
118
+ Likewise, for any lengthy tasks, the UI thread can launch worker threads that
119
+ report back on their progress via the same event queue.
120
+
121
+ Tkinter gives the best of both worlds. It presents the "modern" multithreaded
122
+ GUI model as the primary mode of execution while retaining the freedom and
123
+ flexibility of Tcl's one. Any calls can be made from any threads, only
124
+ subject to Tcl's architectural restictions:
125
+
126
+ * For nonthreaded Tcl, all Tcl calls are wrapped with a global lock. So,
127
+ any calls can be made from any threads, but under the hood, only one
128
+ is active at a time.
129
+
130
+ * A threaded Tcl interpreter instance, when created, becomes tied to the
131
+ creating OS thread ("the interpreter thread"). Tcl mandates that all calls
132
+ to the instance must come from this thread, except special inter-thread
133
+ communication APIs. Tkinter implements calls from outside the interpreter
134
+ thread by posting an event to the interpreter's queue that would run the
135
+ necessary commands, then waits for result. As such:
136
+
137
+ * To make calls from outside the interpreter queue, :func: `mainloop `
138
+ must be running in the interpreter queue. Otherwise, a
139
+ :class: `RuntimeError ` is raised.
140
+
141
+ * A few select functions can only be called from the interpreter thread.
142
+
100
143
101
144
Module contents
102
145
---------------
0 commit comments