[Python] How to pass a python object using user_data with a callback to another function?

Discussão em 'Python' iniciado por Stack, Junho 9, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to use the callback & user_data parameters to call a function and pass an integer to it (in this case it's a list index) but when I print the contents of the passed variable it instead contains the tag of the button the callback originates from.

    An example (without the unnecessary extra stuff):

    import dearpygui.dearpygui as dpg

    for index in range(N):
    with dpg.group(horizontal=True):
    dpg.add_button(label="add", tag="add_btn_"+str(index))
    dpg.set_item_callback(item="add_btn_"+str(index), callback=add_something)
    dpg.set_item_user_data(item="add_btn_"+str(index), user_data=index)


    Where add_something() is just:

    def add_something(user_data):
    print(f"add_something: {user_data}")


    For now, all the function it's calling does is print the user_data argument to the console. I'm expecting that to be the value of index but it actually prints the button tag: add_btn_0 (for example). Even when I set the user_data to a static string it still doesn't pass the correct data.

    Continue reading...

Compartilhe esta Página