12.7. Menu Virtual EventsCurrently, Perl/Tk generates a <<MenuSelect>> virtual event whenever a menu is posted and the active menu item changes.[28] 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, "Anatomy of the MainLoop"). 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; }); Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|