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.
The "user data" will be passed as third positional argument to the callback. In order to be able to use it, you need to define the callback function with 3 parameters.
The first two parameters are for the "sender" and "app data" arguments, you can ignore them if you don't need them.
This is explained in https://dearpygui.readthedocs.io/en/latest/documentation/item-callbacks.html.
In your example, it would look like this:
def add_something(sender, app_data, user_data):
print(f"add_something: {user_data}")