CasuallyCalm / dearpygui-async

MIT License
4 stars 0 forks source link

Handlers don't support async callback functions #1

Open StalkerSanya opened 2 months ago

StalkerSanya commented 2 months ago

When I use async callback in method dpg.additem_handler, I get warning: "RuntimeWarning: coroutine was never awaited"

CasuallyCalm commented 2 months ago

Yea, unfortunately this looks like an issue with DPG and how they handle item handler callbacks, see issue raised here: https://github.com/hoffstadt/DearPyGui/issues/2208

You can work around this in a few ways, but you'll need to specify the task to be run on the dpg_async loop, here's an example:


import random

import dearpygui.dearpygui as dpg
from dearpygui_async import DearPyGuiAsync

HEIGHT = 400
WIDTH = 600

dpg_async = DearPyGuiAsync()

def handler_callback():
    async def async_callback():
        dpg.set_value("text item", random.choice("This is a new value!".split(" ")))
    dpg_async.loop.create_task(async_callback())

def dpg_start():

    dpg.create_context()
    dpg.create_viewport(width=WIDTH, height=HEIGHT)
    dpg.setup_dearpygui()

    with dpg.window(label="Async Example Window", tag="Window", width=WIDTH, height=HEIGHT):
        dpg.add_text("Click Me!", tag="text item")
        with dpg.item_handler_registry(tag="widget handler") as handler:
            dpg.add_item_clicked_handler(callback=handler_callback)
        dpg.bind_item_handler_registry("text item", "widget handler")

    dpg.show_viewport()

if __name__ == "__main__":
    dpg_start()
    dpg_async.run()