hoffstadt / DearPyGui

Dear PyGui: A fast and powerful Graphical User Interface Toolkit for Python with minimal dependencies
https://dearpygui.readthedocs.io/en/latest/
MIT License
12.63k stars 669 forks source link

Add options to listbox data #1236

Closed Methamane closed 1 year ago

Methamane commented 2 years ago

Is your feature request related to a problem? Please describe. When I click on an item in a listbox the data being sent to the callback function is just the string of the item, this is perfectly fine when you have items with unique strings. In my instance I'm using the listbox to display a list of transactions and some of them have identical string names, and then I don't know exactly which of those transactions was selected.

Describe the solution you'd like I think that one of the following would suffice to solve the problem:

  1. Have the callback data be the index of the item selected (perhaps a bool when setting up the listbox, i.e. return_index=True).
  2. Have the list of items given to the listbox as a list of tuples instead of strings, and when an item is selected the data returned would be the corresponding tuple. The first element in the tuple could be the string to display, and the other elements could be custom ids or anything else that is needed.

Describe alternatives you've considered At the moment my solution is just adding the id number in the items string, that works because I can extract the id from the string but the downside is that the id is displayed.

Additional context Here's a minimal example showing that I don't know which of "item1" was clicked.

import dearpygui.dearpygui as dpg

def item_clicked(sender, data):
    print(data)

with dpg.window():
    dpg.add_listbox(items=["item1", "item1", "item2"], callback=item_clicked)

dpg.start_dearpygui()
TheCDC commented 2 years ago

I have this issue as well. The listbox documentation does not seem to provide a canonical way to reference the selected item directly either by identity or by index.

Nonoreve commented 5 months ago

The Pull Request hasn't been merged, why is this closed ? I would really enjoy this feature

bandit-masked commented 5 months ago

It was closed as part of a general clean up, as the number of issues was exceeding the developer time and issues were prioritized. I have flagged the PR today, so maybe it will be picked up again.

In the meantime, you could see if the following example offers any interesting workarounds for you.

https://github.com/hoffstadt/DearPyGui/wiki/Tools-and-Widgets#dpg-swiss-controls