@@ -20,6 +20,16 @@ demonstrating a simple Tk interface, letting you know that :mod:`tkinter` is
2020properly installed on your system, and also showing what version of Tcl/Tk is
2121installed, so you can read the Tcl/Tk documentation specific to that version.
2222
23+ Tkinter supports a range of Tcl/Tk versions, built either with or
24+ without thread support. The official Python binary release bundles Tcl/Tk 8.6
25+ threaded. See the source code for the :mod: `_tkinter ` module
26+ for more information about supported versions.
27+
28+ Tkinter is not a thin wrapper, but adds a fair amount of its own logic to
29+ make the experience more pythonic. This documentation will concentrate on these
30+ additions and changes, and refer to the official Tcl/Tk documentation for
31+ details that are unchanged.
32+
2333.. seealso ::
2434
2535 Tkinter documentation:
@@ -62,6 +72,47 @@ installed, so you can read the Tcl/Tk documentation specific to that version.
6272 Brent Welch's encyclopedic book.
6373
6474
75+ Architecture
76+ ------------
77+
78+ Tcl/Tk is not a single library but rather consists of a few distinct
79+ modules, each with a separate functionality and its own official
80+ documentation. Python's binary releases also ship an add-on module
81+ together with it.
82+
83+ Tcl
84+ Tcl is a dynamic interpreted programming language, just like Python. Though
85+ it can be used on its own as a general-purpose programming language, it is
86+ most commonly embedded into C applications as a scripting engine or an
87+ interface to the Tk toolkit. The Tcl library has a C interface to
88+ create and manage one or more instances of a Tcl interpreter, run Tcl
89+ commands and scripts in those instances, and add custom commands
90+ implemented in either Tcl or C. Each interpreter has an event queue,
91+ and there are facilities to send events to it and process them.
92+ Unlike Python, Tcl's execution model is designed around cooperative
93+ multitasking, and Tkinter bridges this difference
94+ (see `Threading model `_ for details).
95+
96+ Tk
97+ Tk is a `Tcl package <http://wiki.tcl.tk/37432 >`_ implemented in C
98+ that adds custom commands to create and manipulate GUI widgets. Each
99+ :class: `Tk ` object embeds its own Tcl interpreter instance with Tk loaded into
100+ it. Tk's widgets are very customizable, though at the cost of a dated appearance.
101+ Tk uses Tcl's event queue to generate and process GUI events.
102+
103+ Ttk
104+ Themed Tk (Ttk) is a newer family of Tk widgets that provide a much better
105+ appearance on different platforms than many of the classic Tk widgets.
106+ Ttk is distributed as part of Tk, starting with Tk version 8.5. Python
107+ bindings are provided in a separate module, :mod: `tkinter.ttk `.
108+
109+ Tix
110+ `Tix <https://core.tcl.tk/jenglish/gutter/packages/tix.html >`_ is an older
111+ third-party Tcl package, an add-on for Tk that adds several new widgets.
112+ Python bindings are found in the :mod: `tkinter.tix ` module.
113+ It's deprecated in favor of Ttk.
114+
115+
65116Tkinter Modules
66117---------------
67118
@@ -377,6 +428,59 @@ Xlib (C)
377428 the Xlib library to draw graphics on the screen.
378429
379430
431+ Threading model
432+ ---------------
433+
434+ Python and Tcl/Tk have very different threading models, which :mod: `tkinter `
435+ tries to bridge. If you use threads, you may need to be aware of this.
436+
437+ A Python interpreter may have many threads associated with it. In Tcl, multiple
438+ threads can be created, but each thread has a separate Tcl interpreter instance
439+ associated with it. Threads can also create more than one interpreter instance,
440+ though each interpreter instance can be used only by the one thread that created it.
441+
442+ Each :class: `Tk ` object created by :mod: `tkinter ` contains a Tcl interpreter.
443+ It also keeps track of which thread created that interpreter. Calls to
444+ :mod: `tkinter ` can be made from any Python thread. Internally, if a call comes
445+ from a thread other than the one that created the :class: `Tk ` object, an event
446+ is posted to the interpreter's event queue, and when executed, the result is
447+ returned to the calling Python thread.
448+
449+ Tcl/Tk applications are normally event-driven, meaning that after initialization,
450+ the interpreter runs an event loop (i.e. :func: `Tk.mainloop `) and responds to events.
451+ Because it is single-threaded, event handlers must respond quickly, otherwise they
452+ will block other events from being processed. To avoid this, any long-running
453+ computations should not run in an event handler, but are either broken into smaller
454+ pieces using timers, or run in another thread. This is different from many GUI
455+ toolkits where the GUI runs in a completely separate thread from all application
456+ code including event handlers.
457+
458+ If the Tcl interpreter is not running the event loop and processing events, any
459+ :mod: `tkinter ` calls made from threads other than the one running the Tcl
460+ interpreter will fail.
461+
462+ A number of special cases exist:
463+
464+ * Tcl/Tk libraries can be built so they are not thread-aware. In this case,
465+ :mod: `tkinter ` calls the library from the originating Python thread, even
466+ if this is different than the thread that created the Tcl interpreter. A global
467+ lock ensures only one call occurs at a time.
468+
469+ * While :mod: `tkinter ` allows you to create more than one instance of a :class: `Tk `
470+ object (with its own interpreter), all interpreters that are part of the same
471+ thread share a common event queue, which gets ugly fast. In practice, don't create
472+ more than one instance of :class: `Tk ` at a time. Otherwise, it's best to create
473+ them in separate threads and ensure you're running a thread-aware Tcl/Tk build.
474+
475+ * Blocking event handlers are not the only way to prevent the Tcl interpreter from
476+ reentering the event loop. It is even possible to run multiple nested event loops
477+ or abandon the event loop entirely. If you're doing anything tricky when it comes
478+ to events or threads, be aware of these possibilities.
479+
480+ * There are a few select :mod: `tkinter ` functions that presently work only when
481+ called from the thread that created the Tcl interpreter.
482+
483+
380484Handy Reference
381485---------------
382486
0 commit comments