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()
ak.start(task_A(e))
# A1
ak.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.

Files for asynctkinter, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size asynctkinter-0.1.1-py3-none-any.whl (4.0 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size asynctkinter-0.1.1.tar.gz (4.0 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page