insolor / async-tkinter-loop

Asynchronous mainloop implementation for tkinter. Makes it possible to use async functions as event handlers and widget commands.
https://insolor.github.io/async-tkinter-loop/
MIT License
70 stars 3 forks source link

Can use in TaskGroup #80

Closed musimasami closed 9 months ago

musimasami commented 9 months ago

Is this code OK? I used main_loop() function in TaskGroup.

Code from tkinter import * from tkinter import messagebox import asyncio from async_tkinter_loop import async_handler,main_loop

ON_message = 'ON message' OFF_message = 'OFF message'

async def get_ON(OnOff_que): while True: msg = await OnOff_que.get() print(msg)

async def main(): OnOff_que = asyncio.Queue()

root = Tk()
root.title('My Home Temperature')
root.geometry("400x300")

@async_handler
async def on_info():
    res = messagebox.showinfo(title="message", message="ON Aircon")
    await OnOff_que.put(ON_message)
    print(res)
@async_handler
async def off_info():
    res = messagebox.showinfo(title="message", message="OFF Aircon")
    await OnOff_que.put(OFF_message)
    print(res)

Button(root, text='ON', command=on_info).grid()
Button(root, text='OFF', command=off_info).grid()
async with asyncio.TaskGroup() as tg:
    tsk0 = tg.create_task(main_loop(root))
    tsk1 = tg.create_task(get_ON(OnOff_que))

asyncio.run(main())