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.
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