Book HomeMastering Perl/TkSearch this book

7.18. Listbox Virtual Events

The Listbox widget has one virtual event, <<ListboxSelect>>, although it's not available in versions of Perl/Tk prior to 803.000. This event is generated any time the Listbox selection changes. So, when might we bind to this event?[15]

[15] Bindings and callbacks are described in Chapter 15, "Anatomy of the MainLoop".

A common Listbox use is to bind <Button-1> to the widget, with the intent of executing a callback on the button click. Most people then assume that the active Listbox element is the one the button clicked on, but that's wrong, because Tk hasn't yet made that element active (unless it was already active). So if you think you want to do this:

    $mpgs->bind('<1>' => sub {play $mpgs->get('active')});

You probably really want this:

    $mpgs->bind('<<ListboxSelect>>' => sub {play $mpgs->curselection});

But until Tk 803.000 is here, do this instead:

    $mpgs->bind('<ButtonRelease-1>' => sub {play $mpgs->get('active')});

Or even this:

    $mpgs->bind('<1>' => sub {
        play $mpgs->get( $mpgs->nearest($Tk::event->y) )
    });


Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.