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
Release version
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 ashttpx
andpillow
) to run some of the examples. If you're not going to run examples, remove the[examples]
part from the command- Use
pip3
instead ofpip
on 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)
Development version
- Install Poetry, e.g., with
pip install poetry
(pip3 install poetry
) command (other possible ways of installation see here) - Download and unpack or clone the repository.
- Run the command
poetry install
orpoetry install -E examples
(the later command installs optional dependencies needed to run some examples). This command will create.venv
directory with a virtual environment and install dependencies into it.- Run any example with
poetry run python examples/sparks.py
(insert a file name of an example). - Or activate the virtual environment with
poetry shell
and run an example withpython examples/sparks.py
command. You can also open the directory with the project in some IDE (e.g., PyCharm or VS Code) and select Python interpreter from the virtual environment as a project interpreter, then run examples directly from the IDE.
- Run any example with
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
Hashes for async_tkinter_loop-0.9.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7e9d05a7cb1dd17177112a20fb58e69d71c63ae9b4549d9f496252b1926c98ab |
|
MD5 | c284647f4248b7f3643f3911f9357438 |
|
BLAKE2b-256 | 6a0376195babc3db68255d1a28ae298d6f6caf18f735b3fc2acdfde9e48e0e76 |