home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Book HomeMastering Perl/TkSearch this book

12.7. Menu Virtual Events

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

[28] 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.

$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;

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.