Python Forum
[SOLVED] [listbox] Feed it with dict passed to class? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [SOLVED] [listbox] Feed it with dict passed to class? (/thread-42113.html)



[SOLVED] [listbox] Feed it with dict passed to class? - Winfried - May-12-2024

Hello,

This is a newbie question.

Is it possible to feed a listbox with a dictionary (to get text + hidden value) passed through the class as input parameter?

import wx

class MoveItemListBox(wx.ListBox):
  def __init__(self, *args, **kwds):
      """
      #With sampleList_dict:
      TypeError: ListBox(): arguments did not match any overloaded call:
        overload 1: too many arguments
        overload 2: argument 'choices' has unexpected type 'dict'
      """
      super().__init__(*args, **kwds)

      #"choices" is part of **kwds
      #BAD for k, v in items.items():
      #BAD for k, v in items():
      #BAD for k,v in choices.items():
      #BAD for k,v in choices:
        self.Append(k, v);

      self.Bind(wx.EVT_LISTBOX, self.EvtListBox)
      
  def EvtListBox(self, event):
    name = event.GetString()
    data = str(event.GetClientData())
    #self.statusbar.SetStatusText(f"{name}: {data}")
      
class TestFrame(wx.Frame):
  def __init__(self):
      wx.Frame.__init__(self, None, title="Move item in ListBox")
      self.SetSize((370, 400))
      panel = wx.Panel(self)

      sampleList_dict = {'key1': 'value1', 'key2': 'value2', 'item3': 'value3'}
      self.list_box = MoveItemListBox(panel, choices=sampleList_dict, size=(100, 200), style=wx.LB_SINGLE)

      sizer = wx.BoxSizer(wx.HORIZONTAL)
      sizer.Add(self.list_box, 0, wx.EXPAND|wx.RIGHT, 4)
      panel.SetSizer(sizer)
      self.Layout()

if __name__ == '__main__':
  app = wx.App()
  frame = TestFrame()
  frame.Show()
  app.MainLoop()
Thank you.

--
Edit: A work-around is to create a list with just the keys, and use it to fetch the values from the dictionary:
#ListBox doesn't accept dictionaries, only list
data = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
data_keys = list(data)

class TestFrame(wx.Frame):
  def __init__(self):
      wx.Frame.__init__(self, None, title="Move item in ListBox")
      self.SetSize((370, 400))
      panel = wx.Panel(self)

      self.list_box = MoveItemListBox(panel, choices=data_keys, size=(100, 200), style=wx.LB_SINGLE)



RE: [listbox] Feed it with dict passed to class? - Larz60+ - May-12-2024

you can get key and value like:
for key, value in sampleList_dict.items():
    print(key, value)



RE: [listbox] Feed it with dict passed to class? - Winfried - May-13-2024

Thanks. The problem is that a listbox only accepts lists, not dictionaries. So I guess I'll have to keep two objects to work with a listbox. No biggie.


RE: [SOLVED] [listbox] Feed it with dict passed to class? - Larz60+ - May-13-2024

wxpython list box can accept strings one at a time, doesn't have any requirement for a list.
see: InsertItems