I always think i've found something that doesn't work because it doesn't work for me and then i learn better. Hopefully nothing has changed!
Today's frustration is using
'active' as an index in Tk::Menu.
I have a menu which is a list of files, and i want to bind keys so that
multiple actions are available when an entry is active, distinct from the
command which would be invoked by clicking the entry. I thought the way
to do this would be (something like) this:
#!/usr/bin/perl
use strict;
use Tk;
my $MW = MainWindow->new;
my $menu = $MW -> Menu(-type=>'menubar',-tearoff=>0);
$MW -> configure(-menu=>$menu);
$MW -> bind("<Key-F1>"=>[\&printentry]);
my $MM = $menu -> cascade(-label=>'menu',-underline=>0,-tearoff=>0);
my $one = $MM -> command(-label=>'one',-command=>sub{});
my $two = $MM -> command(-label=>'two',-command=>sub{});
my $three = $MM -> command(-label=>'three',-command=>sub{});
MainLoop;
sub printentry {
my $whM = $menu->entrycget('active','-label');
print "active:$whM\t";
}
The problem:
Using a key would seem to be the only way to go, because if you move off
the menu and click anything the menu disappears or their is no real
active entry. Unfortunately, the binding only works when
the first level cascade is active (highlighted), before it is clicked,
and never when any cascade is in use. More unfortunately, 'active' never
returns a value even if "one" is highlighted (active).
Considering this, the use of 'active' as the index,
which is documented,
can never actually apply (except on "one" "two" "three"; but then it doesn't...) Ie, if you substitute '0' or '1' or '2' for 'active', you will get a value.
nb.
if it did work, i believe ->configure could be used to get the same
information from the active cascade (entrycget cannot)
SO PLEASE DON'T TELL ME "$menu isn't $MM" ETC.
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.