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?[1]

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) )
    });

[1] Bindings and callbacks are described in Chapter 15.

Get Mastering Perl/Tk now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.