Listbox and Hashtable troubles

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Katie Beach

    Listbox and Hashtable troubles







    I have a listbox in which I would like to be able to display a value of a hash, but when the user selects an element in the list, I need to be able to use the KEY associated with the value chosen for editing... I don't see an easy way to do this except to create a lookup array that contains an index and a KEY - the index will then be associated with the VALUE in listbox... this seems like a pretty messy way to do this, so I'd like to avoid it if I could. Has anyone else needed something similar, and if so, what was their solution to the problem!!




    Many Thanks,
    Katie






    Click, drag and drop. My MSN is the simple way to design your homepage.
  • Diez B. Roggisch

    #2
    Re: Listbox and Hashtable troubles

    First: Don't use HTML when posting!!!

    Second: What gui-toolkit do you use? Maybe its possible to associate a
    key/value pair to the listbox, where only the value is displayed, but that
    depends on your toolkit.

    Whatever toolkit it is, usually you get the index of the entry the user
    selected - now if you have a dict, you somehow have to enumerate the values
    you get. This order you can use:

    hm = {1 : "one", 2 : "two"}

    items = hm.items() # yields (1, "one"), (2, "two")

    for key, value in items:
    lb.add(value)

    index = lb.get_user_pre ssed_index()
    key = items[index][0]
    --
    Regards,

    Diez B. Roggisch

    Comment

    • wes weston

      #3
      Re: Listbox and Hashtable troubles

      Katie,
      See



      "You can also use a listbox to represent arbitrary Python objects. In
      the next example, we assume that the input data is represented as a list
      of tuples, where the first item in each tuple is the string to display
      in the list. For example, you could display a dictionary by using the
      items method to get such a list.

      self.lb.delete( 0, END) # clear
      for key, value in data:
      self.lb.insert( END, key)
      self.data = data

      When querying the list, simply fetch the items indexed by the selection
      list:

      items = self.lb.cursele ction()
      try:
      items = map(string.atoi , items)
      except ValueError: pass
      items = map(lambda i,d=self.data: d[i], items)"




      Katie Beach wrote:[color=blue]
      > I have a listbox in which I would like to be able to display a value of
      > a hash, but when the user selects an element in the list, I need to be
      > able to use the KEY associated with the value chosen for editing... I
      > don't see an easy way to do this except to create a lookup array that
      > contains an index and a KEY - the index will then be associated with the
      > VALUE in listbox... this seems like a pretty messy way to do this, so
      > I'd like to avoid it if I could. Has anyone else needed something
      > similar, and if so, what was their solution to the problem!!
      >
      >
      > Many Thanks,
      > Katie
      >
      >
      > ------------------------------------------------------------------------
      > Click, drag and drop. My MSN is the simple way to design your homepage.
      > <http://g.msn.com/8HMAENUS/2734??PS=>[/color]

      Comment

      Working...