listcomboboxdearpygui

How to get integer values of a list using input of another list of string values put in combo boxes?


I am relatively new to Dearpygui and having trouble trying to use combo boxes to callback specified int values from a list stored in user_data using the string values of another list in the items of the combo. This code is not my actual code but addresses the problem.

import dearpygui.dearpygui as dpg

dpg.create_context()

listA = ["a", "b", "c"]
listB = [2, 5, 8]

def getvalues(sender, data):
    letter = dpg.get_value("char")
    print(f"Sender: {sender}")
    print(f"Letter: {letter}")
    print(f"Data: {data}")

with dpg.window():
    dpg.add_combo(label="letters", tag="char", items=listA)
    dpg.add_button(label="Get", callback=getvalues, user_data=listB)

dpg.create_viewport(title='Custom Title', width=800, height=600)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

For the letter I get the letter selected in the combo, but data shows up as None and I can't figure out how I should code this.

I tried using .index() for the lists to get indices of ListA and subscript it to ListB. I also tried using dpg.set_value() but that just changed the data of the letter. Please help me out!


Solution

  • Use .index() but get the value from the combo like this:

    listA = ["a", "b", "c"]
    listB = [2, 5, 8]
    
    def getvalues():
        selectlistA = dpg.get_value("char")
        if selectlistA in listA:
            index = listA.index(selectlistA)
            selectlistB = listB[index]
            print(f"value: {selectlistB}")
    
    with dpg.window():
        dpg.add_combo(label="letters", tag="char", items=listA)
        button = dpg.add_button(label="Get", callback=getvalues)