O'Reilly logo

Mastering Perl/Tk by Nancy Walsh, Stephen Lidie

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required