Skip to main content

async library that works on top of tkinter's event loop

Project description

AsyncTkinter

Async library that works on top of tkinter's event loop. (Youtube)

Installation

pip install asynctkinter

Usage

from tkinter import Tk, Label
import asynctkinter as at
at.patch_unbind()

def heavy_task():
    import time
    for i in range(5):
        time.sleep(1)
        print('heavy task:', i)

root = Tk()
label = Label(root, text='Hello', font=('', 60))
label.pack()

async def some_task(label):
    label['text'] = 'start heavy task'

    # wait until a label is pressed
    event = await at.event(label, '<Button>')

    print(event.x, event.y)
    label['text'] = 'running...'

    # create a new thread, run a function on it, then
    # wait for the completion of that thread
    result = await at.run_in_thread(heavy_task, after=label.after)
    print('result of heavytask():', result)

    label['text'] = 'done'

    # wait for 2sec
    await at.sleep(2000, after=label.after)

    label['text'] = 'close the window'


at.start(some_task(label))
root.mainloop()

wait for the completion/cancellation of multiple tasks simultaneously

async def some_task(label):
    from functools import partial
    import asynctkinter as at
    sleep = partial(at.sleep, after=label.after)
    # wait until EITEHR a label is pressed OR 5sec passes
    tasks = await at.or_(
        at.event(label, '<Button>'),
        sleep(5000),
    )
    print("The label was pressed" if tasks[0].done else "5sec passed")

    # wait until BOTH a label is pressed AND 5sec passes"
    tasks = await at.and_(
        at.event(label, '<Button>'),
        sleep(5000),
    )

synchronization primitive

There is a Trio's Event equivalent.

import asynctkinter as at

async def task_A(e):
    print('A1')
    await e.wait()
    print('A2')
async def task_B(e):
    print('B1')
    await e.wait()
    print('B2')

e = at.Event()
at.start(task_A(e))
# A1
at.start(task_B(e))
# B1
e.set()
# A2
# B2

Note

  • Why is patch_unbind() necessary? Take a look at this.

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

asynctkinter-0.1.2.tar.gz (4.0 kB view hashes)

Uploaded Source

Built Distribution

asynctkinter-0.1.2-py3-none-any.whl (4.0 kB view hashes)

Uploaded Python 3

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