Easy multithreading with Tkinter on CPython 2.7/3.x and PyPy 2.7/3.x.
## Background
The Tcl/Tk language is shipped with Python, and follows a
different threading model than Python itself which can
raise obtuse errors when mixing Python threads with Tk, such as:
RuntimeError: main thread is not in main loop
RuntimeError: Calling Tcl from different apartment
NotImplementedError: Call from another thread
Tcl can have many isolated interpreters running, and are
tagged to a particular OS thread. Calling a Tcl interpreter
from a different thread raises an apartment error.
The _tkinter module detect if a Python thread is different
from the Tcl interpreter thread, and then waits one second
to acquire a lock on the main thread. If there is a time-out,
a RuntimeError is raised.
A common approach to avoid these errors involves setting up
periodic polling of a message queue from the Tk main thread, which
can [slow the responsiveness of the GUI.][1]
The approach used in `tkthread` is to use the Tcl/Tk `thread::send`
inter-interpreter messaging to notify the main Tcl/Tk interpreter
of a call for execution.
## Usage
The `tkthread` module provides the `TkThread` class, which can
synchronously interact with the main thread.
from tkthread import tk, TkThread
root = tk.Tk() # create the root window
tkt = TkThread(root) # make the thread-safe callable
import threading, time
def run(func):
threading.Thread(target=func).start()
run(lambda: root.wm_title('FAILURE'))
run(lambda: tkt(root.wm_title, 'SUCCESS'))
root.update()
time.sleep(2) # _tkinter.c:WaitForMainloop fails
root.mainloop()
The `tkt()` instance is callable, and will wait for the main thread
to execute and compute a result, which is then passed back for
return from `tkt()`. A non-synchronous version also exists that
does not block:
tkt.nosync(root.wm_title, 'ALSO SUCCESS')
There is an optional `tkt.install()` method which intercepts
Python-to-Tk calls. This must be called on the default root,
before the creation of child widgets. There is a slight performance
penalty for Tkinter widgets that operate only on the main thread.
The `root` Tcl/Tk interpreter must be the primary interpreter on the
main thread. If it is not, then you will receive a TclError of the form:
_tkinter.TclError: invalid command name "140520536224520_call_from"
Creating several `Tk()` instances and then using TkThread on those will cause this error.
A good practice is to create a root window and then call `root.withdraw()`
to keep the primary Tcl/Tk interpreter active. Future Toplevel windows
use `root` as the master.
## Install
pip install tkthread
## License
Licensed under the Apache License, Version 2.0 (the "License")
[1]: https://www.oreilly.com/library/view/python-cookbook/0596001673/ch09s07.html
Metadata
Release files for tkthread 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tkthread-0.2.0.zip | 11.5 kB | Without provenance | Details for tkthread-0.2.0.zip |
Release files / tkthread-0.2.0.zip
| Download URL | tkthread-0.2.0.zip |
|---|---|
| Size | 11.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
80be7f15e3cf43fb04af3b72c3a5f173383707c92a8ab9042ffbf38dae1b71fb
|
|
BLAKE2b-256 checksum How to use checksums |
ec0492c61030a910b36405f534a6b535537965fb2d900c110d32826530a2a355
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
Python-urllib/3.7
|
Security for tkthread 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.