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
methodkwargs (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, window_title: str = 'Async execution window', window_resizable: ~typing.Tuple[bool, bool] = (False, False), stdout_label_prefix: str = 'Last status: ', show_progress_bar: 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
corohas 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
corois 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
corowill be display though a message box on screen. If you want to obtain the exception though code, you can do so by setting parameterwaitof this function to True and then callingwindow.future.exception().message (Optional[str | Callable[[str], str]]) – Message to be displayed in the window, when
visibleparameter 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.window_title (Optional[str]) – The title of this window. Defaults to “Async execution window”.
window_resizable (Optional[tuple[bool, bool]]) – Controls whether the window is resizable. The first component of this tuple represents the resizable-in-width and second resizable-in-height. Defaults to (False, False), meaning the window is not resizable in any direction.
stdout_label_prefix (Optional[str],) – Only has an effect if the show_stdout parameter is set to True. Each time a message is printed via stdout, the window will show it. This parameter controls the text that is added in front of all messages. Defaults to “Last status: “.
show_progress_bar (Opional[bool],) – Whether to show the progress bar (gauge). Note that this is only a free-running progress bar and it is meant to have a visual effect. It doesn’t allow actual progress to be displayed. Defaults to True.
kwargs (Any) – Any tkinter specific parameters to the TopLevel widget.
- Returns:
The progress TopLevel window responsible for
coroexecution.- Return type:
- Raises:
Exception – Exception that occurred in
coro(if it ocurred). Only raised ifwaitis True.