Menu Virtual Events
Currently, Perl/Tk generates a
<<MenuSelect>>
virtual event whenever
a menu is posted and the active menu item changes.[5] This code binds a callback that
prints on STDOUT the -label
option of the active
menu item. The special variable $Tk::event
is a
localized reference to the X11 event structure (the same thing
returned by a call to XEvent, described in Chapter 15). Its W
method returns the
widget reference that the event occurred in, which is the Menu in
this case. The use of Tk::catch
isn’t
strictly necessary but does prevent the code from exiting if
$menu
isn’t a Menu reference.
Tk::Catch
is essentially a block eval designed to
trap and ignore exceptions.
$menu->bind('<<MenuSelect>>' => sub { my $label = undef; my $menu = $Tk::event->W; Tk::catch {$label = $menu->entrycget('active', -label)}; print "palette=$palette, menu label=$label!\n" if defined $label; });
[5] On Unix, multiple events are generated as the cursor moves over the menu. On Win32, a single event is generated whenever the active menu item changes.
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.