Async library for Tkinter
Project description
AsyncTkinter2
asynctkinter2 はtkinter用のライブラリで、 よくあるasyncライブラリと同じでコールバック関数だらけの醜いコードを読みやすくしてくれます。 例えば
- Aを出力
- 一秒待機
- Bを出力
- labelが押されるまで待機
- Cを出力
といった事を普通にやろうとするとコードは
def やりたき事(label):
bind_id = None
print('A')
def 一秒後に(__):
nonlocal bind_id
print('B')
bind_id = label.bind("<ButtonPress>", labelが押された時に, "+")
label.after(1000, 一秒後に)
def labelが押された時に(event):
label.unbind("<ButtonPress>", bind_id)
print('C')
やりたき事(...)
のように読みにくい物となりますがasynctkinter2を用いることで
import asynctkinter2 as atk
async def やりたき事(label):
print('A')
await atk.sleep(label, 1000)
print('B')
await atk.event(label, "<ButtonPress>")
print('C')
atk.start(やりたき事(...))
と分かりやすく書けます。
Installation
Pin the minor version.
pip install "asynctkinter2>=0.1,<0.2"
使用例
import tkinter as tk
import asynctkinter2 as atk
def main():
root = tk.Tk()
root_task = atk.start(async_main(root))
root.protocol("WM_DELETE_WINDOW", lambda: (root_task.cancel(), root.destroy()))
root.mainloop()
async def async_main(root: tk.Tk):
label = tk.Label(root, text='Hello', font=('', 80))
label.pack()
# 二秒待つ
await atk.sleep(root, 2000)
# labelが押されるのを待つ
event = await atk.event(label, "<ButtonPress>")
print(f"pos: {event.x}, {event.y}")
# labelが押される か 5秒経つまで待つ。
tasks = await atk.wait_any(
atk.sleep(root, 5000),
atk.event(label, "<ButtonPress>"),
)
if tasks[0].finished:
print("5秒経った")
else:
event = tasks[1].result
print(f"labelが押された (pos: {event.x}, {event.y})")
# labelが押され なおかつ 5秒経つまで待つ
tasks = await atk.wait_all(
atk.sleep(root, 5000),
atk.event(label, "<ButtonPress>"),
)
# GUIを固まらせずにHTTPリクエストを実行し、その完了を待つ
import requests
res: requests.Response = await atk.run_in_thread(root, lambda: requests.get("https://httpbin.org/delay/2"))
label["text"] = f"{res.status_code = }"
if __name__ == "__main__":
main()
asynctkinter との違い
現在のasynctkinterは
- メインループは
tkinterが元々持っているmainloop()ではなく独自の物を用い - タイマー機能に関しても
tkinterが元々持っているafter()ではなく独自の物を用いています。
対して asynctkinter2 では独自の物を用いないようにしています。
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file asynctkinter2-0.1.0.tar.gz.
File metadata
- Download URL: asynctkinter2-0.1.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8fcc68214df31013fdf0700bbe0247f7f97c6c2214f7df24f03537073b24905d
|
|
| MD5 |
ff38c3eed1b98fdc1cd51b0e6cd9a0b8
|
|
| BLAKE2b-256 |
11cfa7970dad6994d053566544447ef0aa03692af1be59a92cb286568f2a8ad7
|
Provenance
The following attestation bundles were made for asynctkinter2-0.1.0.tar.gz:
Publisher:
release.yml on asyncgui/asynctkinter2
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
asynctkinter2-0.1.0.tar.gz -
Subject digest:
8fcc68214df31013fdf0700bbe0247f7f97c6c2214f7df24f03537073b24905d - Sigstore transparency entry: 786921980
- Sigstore integration time:
-
Permalink:
asyncgui/asynctkinter2@f3c0d3edc8a156585911b67a7e696a47ffacf553 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/asyncgui
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@f3c0d3edc8a156585911b67a7e696a47ffacf553 -
Trigger Event:
push
-
Statement type:
File details
Details for the file asynctkinter2-0.1.0-py3-none-any.whl.
File metadata
- Download URL: asynctkinter2-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
82f5b24ec0bd74cd38ae3135e6dd7a6c88733b5a2b1be939d1230b8821d5b850
|
|
| MD5 |
8d1fe9b6edb23ba6e9d345be677d814e
|
|
| BLAKE2b-256 |
550e88a7674fc2449b57c48b210f5f169e4411e3097f67fc505f7984c12669f4
|
Provenance
The following attestation bundles were made for asynctkinter2-0.1.0-py3-none-any.whl:
Publisher:
release.yml on asyncgui/asynctkinter2
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
asynctkinter2-0.1.0-py3-none-any.whl -
Subject digest:
82f5b24ec0bd74cd38ae3135e6dd7a6c88733b5a2b1be939d1230b8821d5b850 - Sigstore transparency entry: 786921983
- Sigstore integration time:
-
Permalink:
asyncgui/asynctkinter2@f3c0d3edc8a156585911b67a7e696a47ffacf553 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/asyncgui
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@f3c0d3edc8a156585911b67a7e696a47ffacf553 -
Trigger Event:
push
-
Statement type: