in reply to Re: Tk Menu question.
in thread Tk Menu question.

Hi:)
I missed this out earlier.Consider this bit of code.
map { [Button => $_, -command => sub { replace($_) }] } @menu_items
Is there a way to extract the label of that button in the menu, that the user clicks on,e.g. if the user clicks on
the first button, the label 'Foo', if it is the third button,the label 'Baz', and so on.

So that we can have a replace function like this:
sub replace{ my $chosen="Label of button user clicked on"; + $t->insert('sel.first',"$chosen"); $t->delete('sel.first','sel.last'); $t->tagConfigure("rp",-foreground=>"black"); my $r=$t->search(-forwards,"$chosen",'end'); $t->tagAdd("rp","$r","$r wordend"); } 1;
I need to use a replace function to replace a word marked by the sel tag in a text widget by the label of the menu button.

Or else is it possible to call three subroutines,each being invoked when a particular menu button is clicked.Earlier
my code was of the form:
$m = $top->Menu(-tearoff => 0,font => "{arial} 12 {bold}", -menuitems => [ [Button => "$opt1", -command => \&replace1], [Button => "$opt2", -command => \&replace2], [Button => "$opt3", -command => \&replace3], ] ); sub replace1{ my $chosen1=$menu_items[0]; $t->insert('sel.first',"$chosen1"); $t->delete('sel.first','sel.last'); $t->tagConfigure("rp1",-foreground=>"black"); my $r1=$t->search(-forwards,"$chosen1",'end'); $t->tagAdd("rp1","$r1","$r1 wordend"); } 1; sub replace2{ my $chosen2=$menu_items[1]; ... } 1; sub replace3{ my $chosen3=$menu_items[2]; ... } 1;
I tried this but it keeps printing only the first label, i.e. "Foo", even when we click on other menu buttons.
sub replace{ my $chosen="$menu_items[$_]"; print "\nThe selected word:"; print "\n$chosen"; } 1;
Thanks in advance.
:)

Replies are listed 'Best First'.
Re3: Tk Menu question.
by bbfu (Curate) on Oct 07, 2003 at 12:53 UTC

    Actually, the code I posted is already sending the label to the replace subroutine (that's why it's sub { replace($_) } and not \&replace).

    Just start out your replace sub like this:

    sub replace { my $label = shift; ... }

    bbfu
    Black flowers blossom
    Fearless on my breath

      Hi,
      thanks a lot for your reply.I tried the replace sub like this:
      sub replace{ my $chosen=shift; $chosen= 'none' unless defined $chosen; print "\nThe selected word:"; print "\n$chosen"; } 1;
      I'm getting this as output:
      The selected word: none
      which means the button label is not being sent by the main code to the replace function, is that right? what is the
      problem here?

      Actually I thought that $_ would simply contain the elements of @menu_items sequentially,i.e Foo, Bar,Baz,
      .... Now I take it that clicking on any menu button will call replace($_), where $_ is the label of the button
      on which we click.

      Thanks in advance.
      :)

        Oops. Yeah, sorry about that. I was thinking that it would create a closure, but I forgot that $_ is global so that doesn't work.

        Change the map line to:

          map { [Button => $_, -command => [\&replace, $_]] } @menu_items

        and it should work just fine with that replace sub.

        bbfu
        Black flowers blossom
        Fearless on my breath