Skip to main content

Simple library that allows interaction between Tkinter and asyncio event loop running in a seperate thread.

Project description

Tkinter-Async-Execute is a small library, that provides a way to run an asyncio event loop alongside Tkinter in a separate thread.

It provides a way to execute methods of tkinter widgets from inside async functions and ability to call async functions from functions (commands / events) running in tkinter’s thread. The former can be called from any thread (or async function) and the latter only from functions (commands / events) in tkinter’s thread.

To show progress of an async function, submitted from tkinter, an async execution window widget is available, which will display any text printed with the print() function (or any stdout write requests).

Installation

pip install tkinter-async-execute

Documentation

All documentation is available on https://tkinter-async-execute.readthedocs.io/en/latest/.

Example

from tkinter import ttk
import tkinter as tk
import asyncio

import tk_async_execute as tae


async def async_function():
    # Call tkinter widget methods.
    print("Disabling button")
    tae.tk_execute(bnt.config, state="disabled")  # Thread safe exection
    await asyncio.sleep(5)
    print("Enabling button")
    tae.tk_execute(bnt.config, state="normal")
    await asyncio.sleep(2)

    # Change tkinter text
    print("Renaming button")
    tae.tk_execute(bnt.config, text="Example 2")
    await asyncio.sleep(2)


def button_clicked():
    # Call async function
    tae.async_execute(async_function(), wait=True, visible=True, pop_up=True, callback=None, master=root)

    # Close application
    root.quit()


if __name__ == "__main__":
    root = tk.Tk()
    bnt = ttk.Button(root, text="Click me", command=button_clicked, width=20)
    bnt.pack()

    tae.start()  # Starts the asyncio event loop in a different thread.
    root.mainloop()  # Main Tkinter loop
    tae.stop()  # Stops the event loop and closes it.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tkinter-async-execute-1.0.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

tkinter_async_execute-1.0.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file tkinter-async-execute-1.0.0.tar.gz.

File metadata

  • Download URL: tkinter-async-execute-1.0.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for tkinter-async-execute-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b9360799c03f6f7da163232bc31e84613ee4e4acf9159c463df04b8d30ea9475
MD5 2e818b25a1954f69a004b473e39d579b
BLAKE2b-256 efa16a2eadb0cae02dec57bb2fed40d0d1b70939dd43a975f28d15e713b8813b

See more details on using hashes here.

File details

Details for the file tkinter_async_execute-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for tkinter_async_execute-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 481d5d31583b1e14a61b0431f3e9474d0f16bfe3c19d8c7a82f19993ac98f352
MD5 e6e5192baa3eb7a66d81c15e42b835a6
BLAKE2b-256 7da4eed20b3aa6f19a99351c17d572e667af8ec3ea404c6007d2fda39c52f760

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page