Skip to content

Commit 6cdcb9e

Browse files
committed
* queue is per-thread
1 parent c8f3bca commit 6cdcb9e

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

Doc/library/tkinter.rst

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ installed, so you can read the Tcl/Tk documentation specific to that version.
2121

2222
Tkinter supports a range of Tcl/Tk versions, built either with or
2323
without thread support. The official Python binary release bundles Tcl/Tk 8.6
24-
threaded. See the source code for the underlying :mode:`_tkinter` C module
24+
threaded. See the source code for the :mode:`_tkinter` module
2525
for more information about supported versions.
2626

2727
Tkinter is not a thin wrapper, it adds a fair amount of own logic to
@@ -35,7 +35,7 @@ Architecture
3535

3636
Unlike most other GUI toolkits, Tcl/Tk is not a monolithic product providing a
3737
consolidated API. Instead, it's a bundle of libraries, each with its
38-
separate functionality and documentation.
38+
distinct functionality and separate documentation.
3939

4040
Tcl
4141
Tcl is a dynamic interpreted programming language. Though it can be used
@@ -44,10 +44,11 @@ Tcl
4444
an interface to the Tk toolkit. The Tcl engine library has a C interface to
4545
create and operate interpreter instances, run Tcl commands and scripts with
4646
them and add custom commands that can be implemented in either Tcl or C.
47-
It also implements a per-interpreter event queue. Each :class:`Tk` object
48-
embeds its own interpreter instance. Though :mod:`_tkinter` allows to
49-
execute entire Tcl scripts, the Python bindings typically only run single
50-
commands.
47+
It also implements a per-thread event queue (see `Threading model`_ for
48+
details).
49+
Each :class:`Tk` object embeds its own interpreter instance.
50+
Though :mod:`_tkinter` allows to execute entire Tcl scripts, the Python
51+
bindings typically only run single commands.
5152

5253
Tk
5354
Tk is a Tcl module implemented in C that adds custom commands to create and
@@ -62,7 +63,7 @@ Tk
6263
`tkinter` mostly uses the latter.
6364

6465
Tix
65-
`Tix`<https://core.tcl.tk/jenglish/gutter/packages/tix.html> is a
66+
`Tix`<https://core.tcl.tk/jenglish/gutter/packages/tix.html>_ is a
6667
third-party Tcl module, an addon for Tk that adds several new widgets.
6768
`tkinter.tix` provides bindings for it, and official Python binary releases
6869
come with it bundled. It's deprecated in favor of Ttk.
@@ -71,32 +72,21 @@ Tix
7172
Tkinter Modules
7273
^^^^^^^^^^^^^^^
7374

74-
:mod:`tkinter` has the core functionality and the regular Tk widgets.
75-
Unless you're using the additional widgets, this will be all
75+
:mod:`tkinter` has the core functionality and the bindings for regular Tk
76+
widgets. Unless you're using the additional widgets, this will be all
7677
that you really need.
7778

7879
:mod:`tkinter.ttk` and :mod:`tkinter.tix` have classes for extra
7980
widgets from those families. Ttk is intended to be the new standard widget
8081
set with a more modern look, but as of this writing, it doesn't yet have
8182
replacements for all the classical widgets.
8283

83-
The Tk interface is located in a C module named :mod:`_tkinter`.
84+
The core Tcl/Tk interface is located in a C module named :mod:`_tkinter`.
8485
This module directly interfaces with Tcl/Tk via their C interfaces and
8586
shouldn't be used directly by application programmers save for a few functions.
8687
It is usually a shared library (or DLL), but might in some cases be statically
8788
linked with the Python interpreter.
8889

89-
In addition to the Tk interface module, :mod:`tkinter` includes a number of
90-
Python modules, :mod:`tkinter.constants` being one of the most important.
91-
Importing :mod:`tkinter` will automatically import :mod:`tkinter.constants`,
92-
so, usually, to use Tkinter all you need is a simple import statement::
93-
94-
import tkinter
95-
96-
Or, more often::
97-
98-
from tkinter import *
99-
10090

10191
Threading model
10292
---------------
@@ -118,7 +108,7 @@ predefined "event handler".
118108
Likewise, for any lengthy tasks, the UI thread can launch worker threads that
119109
report back on their progress via the same event queue.
120110

121-
Tkinter strives to provide the best of both worlds. The "moderm" multithreaded
111+
Tkinter strives to provide the best of both worlds. The "modern" multithreaded
122112
GUI model is the primary mode of execution, but pumping messages manually
123113
instead is also supported. What is more important, any Tkinter calls can be
124114
made from any Python threads, only subject to Tcl's architectural restictions:
@@ -141,7 +131,21 @@ made from any Python threads, only subject to Tcl's architectural restictions:
141131

142132
* A few select functions can only be called from the interpreter thread.
143133

144-
134+
Tcl event queue is per-thread rather than per-interpreter. For nonthreaded Tcl,
135+
there's only one global queue shared by all interpreters; for threaded Tcl,
136+
one queue per OS thread that's shared by all interpreters bound to that thread.
137+
So, when an event processing function like :func:`Tk.mainloop` is called, it
138+
will process events not only for the calling :class:`Tk` instance
139+
but for all whose interpreters share the queue.
140+
Events handlers will be called within the same Tcl interpreter that they were
141+
bound to as per the `bind <https://www.tcl.tk/man/tcl8.6/TkCmd/bind.htm>`_ man
142+
page, but any exceptions will be raised from the event processing function.
143+
There's no harm in calling :func:`mainloop` for two :class:`Tk`s at the same
144+
time: with nonthreaded Tcl, they will take turns handling events, and with
145+
threaded Tcl, this is only allowed if the :class:`Tk`s are associated with
146+
different threads anyway.
147+
148+
145149
Module contents
146150
---------------
147151
@@ -218,7 +222,7 @@ Module contents
218222

219223
.. class:: Variable(master=None, value=None, name=None)
220224

221-
Represent a Tcl global variable bound to *master* widget's value via the
225+
Represents a Tcl global variable bound to *master* widget's value via the
222226
`textVariable option
223227
<https://www.tcl.tk/man/tcl8.6/TkCmd/options.htm#M-textvariable>`_.
224228

0 commit comments

Comments
 (0)