Polling selections from a listbox (Tkinter)

Harlin Seritt harlinseritt at yahoo.com
Sat Feb 26 06:48:16 EST 2005


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




More information about the Python-list mailing list