Skip to main content
Yanked

This release has been yanked by its maintainers, and will be ignored by installers, except when explicitly specified.
Consider using release 0.10.4 instead.
Reason given by maintainers: Prerelease

Asynchronous Tkinter Mainloop

Python package

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 helper functions.

Based on ideas from:

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 as aiohttp 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)

Development version

  1. Install Poetry, e.g., with pip install poetry (pip3 install poetry) command (other possible ways of installation see here)
  2. Download and unpack or clone the repository.
  3. Run the command poetry install or poetry install -E examples (the later command installs optional dependencies needed to run some of the 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 and run an example with python 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.

Some examples

Basic example:

import asyncio
import tkinter as tk

from async_tkinter_loop import async_mainloop, async_command


async def counter():
    i = 0
    while True:
        i += 1
        label['text'] = str(i)
        await asyncio.sleep(1.0)


root = tk.Tk()

label = tk.Label(root)
label.pack()

tk.Button(root, text="Start", command=async_command(counter)).pack()

async_mainloop(root)

A more practical example, downloading an image from the Internet with aiohttp and displaying it in the Tkinter window:

import tkinter as tk
from io import BytesIO

import aiohttp
from PIL import Image, ImageTk

from async_tkinter_loop import async_mainloop, async_command


async def load_image(url):
    button['state'] = 'disabled'
    label['image'] = ''
    label['text'] = 'Loading...'

    async with aiohttp.ClientSession() as session:
        response = await session.get(url)
        if response.status != 200:
            label['text'] = f'HTTP error {response.status}'
        else:
            content = await response.content.read()
            pil_image = Image.open(BytesIO(content))
            image = ImageTk.PhotoImage(pil_image)
            label.config(image=image, text='')
            label.image = image
            button['state'] = 'normal'


url = "https://picsum.photos/800/640"

root = tk.Tk()
root.geometry("800x640")

button = tk.Button(root, text='Load an image', command=async_command(load_image, url))
button.pack()

label = tk.Label(root)
label.pack(expand=1, fill=tk.BOTH)

if __name__ == "__main__":
    async_mainloop(root)

More examples see in the examples directory.

Similar projects

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.1.0rc1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

async_tkinter_loop-0.1.0rc1-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file async-tkinter-loop-0.1.0rc1.tar.gz.

File metadata

  • Download URL: async-tkinter-loop-0.1.0rc1.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.8.10 Linux/5.4.0-90-generic

File hashes

Hashes for async-tkinter-loop-0.1.0rc1.tar.gz
Algorithm Hash digest
SHA256 fb05016e1cd8506e07b374c374ec4711e0b91840ab8f8508bd7cb23a06290bd7
MD5 01b01facb7d30a3f7e554d8ad79cbc0c
BLAKE2b-256 eb62d5b7c87742d3a12457cd787e121b2768d1decc6adc90652b39a6acefc532

See more details on using hashes here.

File details

Details for the file async_tkinter_loop-0.1.0rc1-py3-none-any.whl.

File metadata

File hashes

Hashes for async_tkinter_loop-0.1.0rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 6730022bfcfebde7c259afda04457a5d0798b623f88e8f6e28ff7ace065b099a
MD5 37ace6cf59d840eacd2001d288b9e03c
BLAKE2b-256 b916a5febfdaadd34e0708cdb40ee42cf580c79acfefe284d9c2bc2c9c75b6d6

See more details on using hashes here.

Release history Release notifications | RSS feed

0.10.4

2 files

0.10.3

2 files

0.10.2

2 files

0.10.1

2 files

0.10.0

2 files

0.9.3

2 files

0.9.2

2 files

0.9.1

2 files

0.9.0

2 files

0.8.1

2 files

0.8.0

2 files

0.7.0

2 files

0.6.0

2 files

0.5.0

2 files

0.4.0

1 file

0.3.0

2 files

0.2.0

2 files

0.1.0

2 files

This release

0.1.0rc1 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page