Asynchronous Tkinter Mainloop
Implementation of asynchronous mainloop for tkinter, the use of which allows using async handler functions.
It is intended to be as simple to use as possible. No fancy unusual syntax or constructions - just use an alternative
function instead of root.mainloop() and wrap asynchronous handlers into a helper function.
Note
Please, fill free to report bugs, add pull requests or share your thoughts / ask questions, etc. about the module.
Based on ideas from:
- my answer on ru.stackoverflow.com: https://ru.stackoverflow.com/a/1043146
- answer of Terry Jan Reedy on stackoverflow.com: https://stackoverflow.com/a/47896365
- answer of jfs on ru.stackoverflow.com: https://ru.stackoverflow.com/a/804609
Installation
Install the package with the following command:
pip install async-tkinter-loop
or
pip install async-tkinter-loop[examples]
[examples]part is needed to install optional dependencies (such ashttpxandpillow) to run some of the examples. If you're not going to run examples, remove the[examples]part from the command- Use
pip3instead ofpipon Linux systems to install the package for python3 (not python2) - Probably you'll want to create a virtual environment for experiments with this library, but this is optional.
- If you want to try examples, download the entire repository as an archive (green "code" button on the GitHub page → "Download ZIP"), unpack, run any example (of course, you need to install optional dependencies)
Some examples
Basic example:
import asyncio
import tkinter as tk
from async_tkinter_loop import async_handler, async_mainloop
async def counter():
i = 0
while True:
i += 1
label.config(text=str(i))
await asyncio.sleep(1.0)
root = tk.Tk()
label = tk.Label(root)
label.pack()
tk.Button(root, text="Start", command=async_handler(counter)).pack()
async_mainloop(root)
Also, async_handler function can be used as a decorator (but it makes a decorated function syncroneous):
import asyncio
import tkinter as tk
from async_tkinter_loop import async_handler, async_mainloop
@async_handler
async def counter():
i = 0
while True:
i += 1
label.config(text=str(i))
await asyncio.sleep(1.0)
root = tk.Tk()
label = tk.Label(root)
label.pack()
tk.Button(root, text="Start", command=counter).pack()
async_mainloop(root)
A more practical example, downloading an image from the Internet with httpx (you can use aiohttp as well) and displaying it in the Tkinter window:
import tkinter as tk
from io import BytesIO
import httpx
from PIL import Image, ImageTk
from async_tkinter_loop import async_handler, async_mainloop
async def load_image(url):
button.config(state=tk.DISABLED)
label.config(text="Loading...", image="")
async with httpx.AsyncClient() as client:
response = await client.get(url, follow_redirects=True)
if response.status_code != 200:
label.config(text=f"HTTP error {response.status_code}")
else:
content = response.content
pil_image = Image.open(BytesIO(content))
image = ImageTk.PhotoImage(pil_image)
label.image = image
label.config(image=image, text="")
button.config(state=tk.NORMAL)
url = "https://picsum.photos/800/640"
root = tk.Tk()
root.geometry("800x640")
button = tk.Button(root, text="Load an image", command=async_handler(load_image, url))
button.pack()
label = tk.Label(root)
label.pack(expand=1, fill=tk.BOTH)
async_mainloop(root)
More examples see in the examples directory.
Metadata
Release files for async-tkinter-loop 0.10.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| async_tkinter_loop-0.10.4.tar.gz | 5.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| async_tkinter_loop-0.10.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.4 kB
Release files / async_tkinter_loop-0.10.4.tar.gz
| Download URL | async_tkinter_loop-0.10.4.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cb88033975e4e33d6001055e07efe03b389ae127c8089897578ee975a110469e
|
|
BLAKE2b-256 checksum How to use checksums |
011abb49f6d7f63a294a1a3f0cd5531a9fb9f5512400c427f9fd8a293818dc97
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.13
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 17, 2026.
Transparency logRelease files / async_tkinter_loop-0.10.4-py3-none-any.whl
| Download URL | async_tkinter_loop-0.10.4-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7d5e0c5f98c54fa4c74436148a7bf6707d647d3022ec402c8d452d32a524c414
|
|
BLAKE2b-256 checksum How to use checksums |
ba6769c69861ef8467d5df2f0763781f253dffae0877b5f2f704392832b10e07
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.13
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 17, 2026.
Transparency log