Skip to main content

Asynchronous mainloop implementation for tkinter

Project description

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]
  • [examples] part is needed to install optional dependencies (such as httpx and pillow) to run some of the examples. If you're not going to run examples, remove the [examples] part from the command
  • Use pip3 instead of pip 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)

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

async_tkinter_loop-0.9.3.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

async_tkinter_loop-0.9.3-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file async_tkinter_loop-0.9.3.tar.gz.

File metadata

  • Download URL: async_tkinter_loop-0.9.3.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.12 Linux/6.2.0-1016-azure

File hashes

Hashes for async_tkinter_loop-0.9.3.tar.gz
Algorithm Hash digest
SHA256 509c418139847bcb2e47a5a6b6d24a2e2dca290bc468dad6b6b8029e8a865bfd
MD5 bbac9beadf4f729e6112a91e4298b2bd
BLAKE2b-256 4acd6d61fb996178deabf8b7cce8d815986c1cc5eb7bb8aaed7085eccc76906b

See more details on using hashes here.

File details

Details for the file async_tkinter_loop-0.9.3-py3-none-any.whl.

File metadata

  • Download URL: async_tkinter_loop-0.9.3-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.12 Linux/6.2.0-1016-azure

File hashes

Hashes for async_tkinter_loop-0.9.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4c69f46ffdbac48dc44c296c3a7b00c4cc4f852c3d43aa2fa329991d1fadea02
MD5 8bc3cae04c513166659331c3a3e7ba83
BLAKE2b-256 8167c0cf42e835906979d6afbb8dc909ad11ada175340f09e950606b559c1a85

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page