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.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access