from tkinter import Tk, Label
import asynctkinter as at

at.patch_unbind()

root = Tk()
label = Label(root, font=('', 40))
label.pack()

async def async_func(label):
    from functools import partial
    sleep = partial(at.sleep, after=label.after)
    while True:
        label['text'] = 'Press the left mouse button!!'
        await at.event(label, '<Button-1>')
        label['text'] = 'One more time'
        await at.event(label, '<Button>', filter=lambda event: event.num == 1)
        label['text'] = 'Nice!!'
        await sleep(1500)
        label['text'] = 'Press the right mouse button!!'
        await at.event(label, '<Button-3>')
        label['text'] = 'One more time'
        await at.event(label, '<Button>', filter=lambda event: event.num == 3)
        label['text'] = 'Great!!'
        await sleep(1500)

at.start(async_func(label))
root.mainloop()
 def start_async(self, task):
     at.start(task)
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)
    return 'done'


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


async def some_task(label):
    label['text'] = 'start heavy task'
    event = await at.event(label, '<Button>')
    label['text'] = 'running...'
    result = await at.run_in_thread(heavy_task, after=label.after)
    label['text'] = result
    await at.sleep(2000, after=label.after)
    label['text'] = 'close the window'


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

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


async def animate_label(label):
    from functools import partial
    sleep = partial(at.sleep, after=label.after)
    await sleep(1500)
    while True:
        label['text'] = 'This animation'
        await sleep(1500)
        label['text'] = 'can be cancelled anytime'
        await sleep(2000)
        label['text'] = 'by clicking the label'
        await sleep(2500)


async def root_func(label):
    await at.or_(
        animate_label(label),
        at.event(label, '<Button>'),
    )
    label['text'] = 'Cancelled!!'


at.start(root_func(label))
root.mainloop()
Example #5
0
from tkinter import Tk, Label
import asynctkinter as at

at.patch_unbind()

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


async def animate_label(label):
    from functools import partial
    sleep = partial(at.sleep, after=label.after)
    await sleep(2000)
    while True:
        label['text'] = 'Do'
        await sleep(500)
        label['text'] = 'You'
        await sleep(500)
        label['text'] = 'Like'
        await sleep(500)
        label['text'] = 'Tkinter?'
        await sleep(2000)
        label['text'] = 'Answer me!'
        await sleep(2000)


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