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?
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:
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?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
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() |
--
Edit: A work-around is to create a list with just the keys, and use it to fetch the values from the dictionary:
1 2 3 4 5 6 7 8 9 10 11 |
#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) |