I was too lazy to upgrade from the fedora 7 standard (Tk804.027) but the class binding works! However,
$menu->entrycget('active',-label) still won't work all by itself, and
$menu->index('active') doesn't either. PLUS of course entrycget can only be applied to a Menu and not a cascade (who would have thot they should be different?)
So this rather different version does work, it seems simpler to me than getting into these "clone" things:
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
my $MW = MainWindow->new;
my $menu = $MW -> Menu(-type=>'menubar',-tearoff=>0);
$MW -> configure(-menu=>$menu);
$MW -> bind('Tk::Menu',"<Key-F1>"=>[\&printentry]); # the class bindin
+g
my %MM = (); my %MC = (); my %ME = ();
$MM{one} = $MW -> Menu(-tearoff=>0);
$MC{one} = $menu -> cascade(-menu=>$MM{one},-label=>'one',-underline=>
+0,-tearoff=>0);
$ME{Ia} = $MM{one} -> command(-label=>'Ia',-command=>sub{exit});
$ME{Ib} = $MM{one} -> command(-label=>'Ib',-command=>sub{exit});
$ME{Ic} = $MM{one} -> command(-label=>'Ic',-command=>sub{exit});
$MM{two} = $MW -> Menu(-tearoff=>0);
$MC{two} = $menu -> cascade(-menu=>$MM{two},-label=>'two',-underline=>
+0,-tearoff=>0);
$ME{IIa} = $MM{two} -> command(-label=>'IIa',-command=>sub{exit});
$ME{IIb} = $MM{two} -> command(-label=>'IIb',-command=>sub{exit});
$ME{IIc} = $MM{two} -> command(-label=>'IIc',-command=>sub{exit});
my $label; my %name=();
$menu -> bind('<<MenuSelect>>' => sub { # works only on "Menu"
$label = undef; # NOT "cascade"
my $this = $Tk::event->W;
Tk::catch {$label = $this->entrycget('active',-label)};
});
my $x;
foreach $x (keys %MM) {
$MM{$x} -> bind('<<MenuSelect>>' => sub {
$name{$x} = undef;
my $that = $Tk::event->W;
Tk::catch {$name{$x} = $that->entrycget('active',-labe
+l)};
});
}
MainLoop;
sub printentry {
print "hello $label\t$name{$label}\n"
}
The core of the solution is
<<MenuSelect>>, which gets a promising single reference in Tk::Menu,
Whenever a menu's active entry is changed, a <<MenuSelect>> virtual
event is sent to the menu. The active item can then be queried from the
menu, and an action can be taken, such as setting context-sensitive
help text for the entry.
Oh really! Not with the aforementioned
$menu->index('active') it can't -- that just returns "none". But the people who wrote the O'Reilly Perl/Tk book managed; lucky for me a page on this comes up as a web sample because i don't have the book. That's where i got the four lines of the sub with <<MenuSelect>> in it.
My question now for anyone who has stuck with me thus far and understands better what is going on is:
What does $Tk::event->W do?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.