Execution#

tk_execute#

tk_async_execute.utils.tk_execute(method: Callable, *args, **kwargs)#

Allows thread-safe execution of tkinter methods.

Parameters:
  • method (Callable) – A tkinter widget method. Methods that are not from tkinter widgets, should be called directly without this function.

  • args (Any) – Positional arguments to pass to method

  • kwargs (Any) – Keyword arguments to pass to method

async_execute#

tk_async_execute.utils.async_execute(coro: ~typing.Coroutine, wait: bool = True, visible: bool = True, pop_up: bool = False, callback: ~typing.Callable | None = None, show_exceptions: bool = True, message: str | ~typing.Callable[[str], str] | None = <function <lambda>>, show_stdout: bool = True, **kwargs)#

Executes a coroutine inside asyncio event loop.

Call this from tkinter callbacks.

Parameters:
  • coro (Coroutine) – The coro to run.

  • wait (Optional[bool]) –

    Wait until the execution of coro has completed, before returning from this function. Defaults to True.

    WARNING If multiple executions are happening at once, due to the way tkinter’s event loop works, this will wait for all executions to finish before returning from this function in the LIFO manner (last execution will be exited first).

  • visible (Optional[bool]) – Show the execution progress through a new window. Defaults to True.

  • pop_up (Optional[bool]) – If True, all other windows will be blocked from interactions, until the execution of coro is complete. Defaults to False.

  • callback (Optional[Callable]) – Callback function to call with result afer coro has finished. Defaults to None.

  • show_exception (Optional[bool]) – If True, any exceptions that ocurred in coro will be display though a message box on screen. If you want to obtain the exception though code, you can do so by setting parameter wait of this function to True and then calling window.future.exception().

  • message (Optional[str | Callable[[str], str]]) – Message to be displayed in the window, when visible parameter is set to True. When a string (str) is provided, it will be displayed as the message. If a callable is provided, it will be called with the function name as a parameter, and it must return a string that will be used as the displayed message.

  • show_stdout (Optional[bool]) – If True, any write to stdout (e.g., the print() function) will be displayed in the window when the execution window is shown (visible=True). Defaults to True.

  • **kwargs – Any tkinter specific parameters to the TopLevel widget.

Returns:

The progress TopLevel window responsible for coro execution.

Return type:

ExecutingAsyncWindow

Raises:

Exception – Exception that occurred in coro (if it ocurred). Only raised if wait is True.