Polling selections from a listbox (Tkinter)

H

Harlin Seritt

I am trying to poll selections from a listbox but can't seem to get it
to work correctly.

class pollstuff:
def __init__(self, master):
self.listbox = Listbox(master)
self.listbox.pack()

names = ['Bob', 'Neal', 'Mike']
for n in names:
self.listbox.insert(END, n)
self.listbox.select_set(0)

LabelStr = StringVar()
self.label = Label(master, textvariable=LabelStr)
self.label.pack()
LabelStr.set(names[map(int, self.listbox.curselection())])

root = Tk()
app = pollstuff(root)
root.mainloop()

Obviously when this starts up this is going to show selection #0 inside
the label box. How do I make sure that whatever is arbitrarily selected
ends up in the label box as this gui runs? I tried doing a bind:

self.listbox.bind("<Button-1>", self.changelabel)

This did not work as intended as the changelabel's text option only
showed what the selection index was BEFORE the event took place. Does
anyone know of any special thing I need to do in order to poll the
listbox items?

Thanks,

Harlin
 
E

Eric Brunel

On 26 Feb 2005 03:48:16 -0800 said:
Obviously when this starts up this is going to show selection #0 inside
the label box. How do I make sure that whatever is arbitrarily selected
ends up in the label box as this gui runs? I tried doing a bind:

self.listbox.bind("<Button-1>", self.changelabel)

This did not work as intended as the changelabel's text option only
showed what the selection index was BEFORE the event took place.

Bind on <ButtonRelease-1>; <Button-1> is a synonym for <ButtonPress-1>, and when the button is pressed, the selection is not done yet.

HTH
 

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

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top