Tkinter listbox selection handling

Tim Daneliuk tundra at tundraware.com
Sat Nov 9 18:48:05 EST 2002


Hmmm ...  I have Tkinter listbox created like this:

class myUI:

     def __init__(self, root):

         # Setup the visual elements
         self.hSB = Scrollbar(root, orient=HORIZONTAL)
         self.vSB = Scrollbar(root, orient=VERTICAL)
         self.listbox = Listbox(root,
                                foreground = FCOLOR,
                                background  = BCOLOR,
                                font = (FNAME, FSZ, FWT),
                                selectmode=SINGLE,
                                exportselection=0,
                                xscrollcommand=self.hSB.set,
                                yscrollcommand=self.vSB.set,
                                height = HEIGHT,
                                width = WIDTH,
                                )

         self.hSB.config(command=self.y.xview)
         self.hSB.pack(side=BOTTOM, fill=X)
         self.vSB.config(command=self.DirList.yview)
         self.vSB.pack(side=RIGHT, fill=Y)
         self.DirList.pack(side=LEFT, fill=BOTH, expand=1)


I instantiate it like this:

root = Tk()
UI = myUI(root)

Then I add a handler:

UI.listbox.bind('<button-1>', handlerfunc)

All this works fine up to a point.  However, inside handlerfunc(), if I do
a UI.listbox.selection_get(,) I get the *previously* selected item back,
not the currently selected item.  This is also the case if I try using
UI.listbox.curselection().

I've worked around this by trashing the idea of an event handler and
polling the widget regularly looking for changes, but that is a really
ugly solution... This was suggested in some of my reading because there
is no 'command' attribute for a Listbox.

So, am I missing something here? *Is* there a way to use events to
pickup the currently selected Listbox item, or am I stuck doing it with
polling?

TIA,
------------------------------------------------------------------------------
Tim Daneliuk
tundra at tundraware.com




More information about the Python-list mailing list