Asynchronous mainloop implementation for tkinter
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file async_tkinter_loop-0.10.3.tar.gz.
File metadata
- Download URL: async_tkinter_loop-0.10.3.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2728348e54585bd72c305dd9b2572f089c83ab7fe0bf1fb97b9f6c4c2b7b8ef1
|
|
| MD5 |
9353f7ef2312cea7a752ffcfb801330b
|
|
| BLAKE2b-256 |
5341cc8f80456cfb2c294b544fb0c2fe9d74778a12b8e571aafeecca1d6e7247
|
Provenance
The following attestation bundles were made for async_tkinter_loop-0.10.3.tar.gz:
Publisher:
pypi-deploy.yml on insolor/async-tkinter-loop
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
async_tkinter_loop-0.10.3.tar.gz -
Subject digest:
2728348e54585bd72c305dd9b2572f089c83ab7fe0bf1fb97b9f6c4c2b7b8ef1 - Sigstore transparency entry: 702399943
- Sigstore integration time:
-
Permalink:
insolor/async-tkinter-loop@c7ce2e3b6499670643ea5a4b7ee1773a70d5e1ec -
Branch / Tag:
refs/tags/0.10.3 - Owner: https://github.com/insolor
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-deploy.yml@c7ce2e3b6499670643ea5a4b7ee1773a70d5e1ec -
Trigger Event:
release
-
Statement type:
File details
Details for the file async_tkinter_loop-0.10.3-py3-none-any.whl.
File metadata
- Download URL: async_tkinter_loop-0.10.3-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b97a1018613a6b7c7fc48f25fd93d25945ebf7c271182d98235feb1d2753908a
|
|
| MD5 |
d629e955c09220beb38a2c76a41ac6b0
|
|
| BLAKE2b-256 |
3912c281ecb4462b1daacec312a74460eddeb01064fdf77fb78504b105a6f1df
|
Provenance
The following attestation bundles were made for async_tkinter_loop-0.10.3-py3-none-any.whl:
Publisher:
pypi-deploy.yml on insolor/async-tkinter-loop
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
async_tkinter_loop-0.10.3-py3-none-any.whl -
Subject digest:
b97a1018613a6b7c7fc48f25fd93d25945ebf7c271182d98235feb1d2753908a - Sigstore transparency entry: 702399944
- Sigstore integration time:
-
Permalink:
insolor/async-tkinter-loop@c7ce2e3b6499670643ea5a4b7ee1773a70d5e1ec -
Branch / Tag:
refs/tags/0.10.3 - Owner: https://github.com/insolor
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-deploy.yml@c7ce2e3b6499670643ea5a4b7ee1773a70d5e1ec -
Trigger Event:
release
-
Statement type: