Skip to content

get_value returns a string representation instead of the object itself #2291

@Nonoreve

Description

@Nonoreve

Version of Dear PyGui

Version: 1.9.1
Operating System: Manjaro (using virtual env)

My Issue/Question

For UI items holding a list of things to display (like listboxes), we can use the argument items=() to give them arbitrary python objects to be displayed. dpg.add_listbox(items=items, num_items=len(items))
When displaying, the string representation of these objects is used to show these objects.
When using get_value(listbox), the result is always a string and not the object that was given.

The problem is that I want to use a listbox to display a list of object whose string representation is the same, and select one from the list to see its details.
Because of the str return from get_value(), I have no way to tell which item was selected without inserting information (like indices) directly in the string displayed by the item.

To Reproduce

Run the code below, select an item from the list and click the button. The results are printed to the console.

Expected behavior

From what I saw I dearpygui's source code, the value is directly converted when creating the item or setting values, and stored as a string. It could be changed to store the python object and only get the string representation when drawing. Then, when getting the value, return the orginal, untouched object.

Standalone, minimal, complete and verifiable example

This is a stripped version of my app. I made sure it runs well.

import dearpygui.dearpygui as dpg


class Entry:
	def __init__(self, entry):
		self.entry = entry

	def __repr__(self):
		print(f"__repr__ called on {id(self)}")
		return "AAAAA"


def compare_callback(sender, app_data, user_data):
	items = user_data
	selection = dpg.get_value(duplicate_documents_listbox)
	print(f"selection={(selection)},  type={type(selection)}, id={id(selection)}")


dpg.create_context()
dpg.create_viewport()
dpg.setup_dearpygui()

with dpg.window(no_close=True, modal=True):
	dpg.add_text(label='duplicate_text', default_value='There are similar entries already in the database :')
	items = [Entry("a"), Entry("b"), Entry(1), Entry("a"), Entry("42"), Entry(True)]
	duplicate_documents_listbox = dpg.add_listbox(label="", tag='duplicate_documents', items=items, default_value='', num_items=len(items))
	dpg.add_button(label="Compare new and selected one", user_data=(items), tag='compare_entries', callback=compare_callback)


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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions