By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,270 Members | 1,703 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,270 IT Pros & Developers. It's quick & easy.

Listbox and Hashtable troubles

P: n/a




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.
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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]
--
Regards,

Diez B. Roggisch
Jul 18 '05 #2

P: n/a
Katie,
See
http://www.pythonware.com/library/tk...3-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[i], items)"


Katie Beach wrote:
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=>


Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.