Python Forum
[SOLVED] [listbox] Feed it with dict passed to class?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] [listbox] Feed it with dict passed to class?
#1
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)
Reply
#2
you can get key and value like:
for key, value in sampleList_dict.items():
    print(key, value)
Winfried likes this post
Reply
#3
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.
Reply
#4
wxpython list box can accept strings one at a time, doesn't have any requirement for a list.
see: InsertItems
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation Multi-Threaded Camera Feed issue Khajababa69 0 266 May-05-2024, 09:44 PM
Last Post: Khajababa69
  How to receive two passed cmdline parameters and access them inside a Python script? pstein 2 433 Feb-17-2024, 12:29 PM
Last Post: deanhystad
  Draw bounding boxes on live feed Jerome 0 382 Jan-20-2024, 10:50 PM
Last Post: Jerome
Question [solved] Classes, assign an attributes to a class not to instances.. SpongeB0B 4 1,034 May-20-2023, 04:08 PM
Last Post: SpongeB0B
  [Solved] Novice question to OOP: can a method of class A access attributes of class B BigMan 1 1,375 Mar-14-2022, 11:21 PM
Last Post: deanhystad
  dict class override: how access parent values? Andrey 1 1,711 Mar-06-2022, 10:49 PM
Last Post: deanhystad
  How to parse a live feed in Python? Daring_T 2 4,318 Jan-20-2022, 04:17 AM
Last Post: Daring_T
  detecting a generstor passed to a funtion Skaperen 9 3,777 Sep-23-2021, 01:29 AM
Last Post: Skaperen
  Feed List items with Integer euras 9 4,141 May-19-2021, 07:45 PM
Last Post: snippsat
  splitting UAV/sat images to smaller pieces in order to feed a CNN hobbyist 0 1,573 Dec-08-2020, 11:48 AM
Last Post: hobbyist

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020