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?
--
Edit: A work-around is to create a list with just the keys, and use it to fetch the values from the dictionary:
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)