Library API
async_handler(async_function, *args, **kwargs)
A helper function which allows to use async functions as command handlers (e.g. button click handlers) or event handlers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
async_function |
Callable[P, Coroutine[Any, Any, None]]
|
async function |
required |
args |
positional parameters which will be passed to the async function |
()
|
|
kwargs |
keyword parameters which will be passed to the async function |
{}
|
Returns:
Type | Description |
---|---|
Callable[P, None]
|
A sync function, which runs the original async function in an async event loop. |
Usage examples:
import tkinter as tk
from async_tkinter_loop import async_handler
async def some_async_function():
print("Wait...")
await asyncio.sleep(0.5)
print("Done!")
button = tk.Button("Press me", command=async_handler(some_async_function))
# ----
async def some_async_function(event):
print("Wait...")
await asyncio.sleep(0.5)
print("Done!")
root.bind("<1>", command=async_handler(some_async_function))
# ----
# Also, it can be used as a decorator
@async_handler
async def some_async_function():
print("Wait...")
await asyncio.sleep(0.5)
print("Done!")
button = tk.Button("Press me", command=some_async_function)
Source code in async_tkinter_loop/async_tkinter_loop.py
async_mainloop(root)
A function, which is a substitute to the standard root.mainloop()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root |
Tk
|
tkinter root object |
required |
get_event_loop()
A helper function which returns an event loop using current event loop policy.
Returns:
Type | Description |
---|---|
AbstractEventLoop
|
event loop |
main_loop(root)
async
An asynchronous implementation of tkinter mainloop. The function is not intended to be called directly from your code.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root |
Tk
|
tkinter root window object |
required |