Search code examples
pythontkinterlistbox

Python Tkinter: Insert objects into listbox, and then get them out (not as string)


I have a listbox that displays some objects:

# extra is some array of 'extra' objects I get from a database
self.extrasList = Listbox(self, selectmode='multiple')
self.extrasList.pack(fill=X, expand=True)
for extra in extras:
    self.extrasList.insert(END, extra)

The extra objects have their __str__ overridden, so they display just fine.

However, when I then later do:

selection = self.extrasList.curselection()
extra = self.extrasList.get(selection[0]) #this is now a string rather than the proper 'extra' object

Is there a way to get the object back out of the listbox? Rather than a string?

The only thing I could find was to manually keep the listbox and an other list in sync. But this can be quite errorprone.


Solution

  • No, there is no way to get the data out of a listbox as anything other than a string.