Code Comments
Programming Forum and web based access to our favorite programming groups.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 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, using my method 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. Or so it it seems to me.
nb. IF IT DID WORK, i believe ->configure could be used to get the same
information from the active cascade (entrycget cannot).
Post Follow-up to this messageOn Mar 7, 7:46 pm, MK <halfcountp...@intergate.com> wrote:
> 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 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, using my method 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. Or so it it seems to me.
>
> nb. IF IT DID WORK, i believe ->configure could be used to get the same
> information from the active cascade (entrycget cannot).
Your binding is on $MW only. To bind to all children and send the
active window, do:
$MW -> bind('all', "<Key-F1>"=>[\&printentry, Tk('W') ]);
sub printentry {
my $wgt = shift;
my $whM = $wgt->entrycget('active','-label');
print "active:$wgt :: $whM\n";
}
This does what you want, but throws an error if no menu entry is
active - I'll leave that as an exercise for the advanced user.
--S
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.