insolor / async-tkinter-loop

Asynchronous mainloop implementation for tkinter. Makes it possible to use async functions as event handlers and widget commands.
https://insolor.github.io/async-tkinter-loop/
MIT License
61 stars 3 forks source link
aiohttp asyncio customtkinter httpx tkinter

Asynchronous Tkinter Mainloop

Python tests Documentation Coverage Status Maintainability PyPI Supported Python versions

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:

Installation

Install the package with the following command:

pip install async-tkinter-loop

or

pip install async-tkinter-loop[examples]

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.