Listbox and Hashtable troubles

K

Katie Beach

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.
 
D

Diez B. Roggisch

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_pressed_index()
key = items[index][0]
 
W

wes weston

Katie,
See
http://www.pythonware.com/library/tkinter/introduction/x5453-patterns.htm


"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.curselection()
try:
items = map(string.atoi, items)
except ValueError: pass
items = map(lambda i,d=self.data: d, items)"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top