Can't add clicked handler for drawn figures using DearPyGui

1.2k Views Asked by At

I can't add clicked handler for drawn figures, in this case it circle. I tried same, but for text and it work correctly, but for rectangle don't work too.

Here is the code that doesn't work:

import dearpygui.dearpygui as dpg


def change_text():
    print("Clicked")


with dpg.window(width=500, height=300):
    with dpg.drawlist(width=200, height=200):
        dpg.draw_circle([100, 100], 100, fill=[255, 255, 255, 100], id="Circle_id")

    dpg.add_clicked_handler("Circle_id", callback=change_text)

dpg.start_dearpygui()

And another one that work:

import dearpygui.dearpygui as dpg


def change_text():
    print("Clicked")


with dpg.window(width=500, height=300):
    dpg.add_text("some text", id="Text_id")

    dpg.add_clicked_handler("Text_id", callback=change_text)

dpg.start_dearpygui()

Or if you know that there is the way to add round image button or just round button, please write how to do it.

1

There are 1 best solutions below

0
On

I guess that circles (drawings) are not items per say. I tried registering an item handler to the Circle_id, but resulted in error.

Registering an item handler with drawlist works just fine, but the click is in this 200x200 defined area of the drawlist.

import dearpygui.dearpygui as dpg

dpg.create_context()

def change_text():
    print("Clicked!")


with dpg.window(width=500, height=300):
    with dpg.drawlist(width=200, height=200, tag="drawlist"):
        dpg.draw_circle([100, 100], 100, fill=[255, 255, 255, 100], tag="Circle_id", parent="drawlist" )

    with dpg.item_handler_registry(tag="widget handler"):
        dpg.add_item_clicked_handler(callback=change_text)

    dpg.bind_item_handler_registry("drawlist", "widget handler")

dpg.create_viewport(title="Viewport name")

dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

I think that all the handlers are moved to the global handler registry. This works for me and registers when I click on the area of the drawlist, and does not register when clicked on another part of the canvas.