How to get caller widget from subroutine in Perl / TK?

in perl Tk I want to ask how to get the calling widget when using the -command option.

I have a dialog widget dynamically created with a previously unknown number of entries. There is an input widget next to each button, where you can view possible (useful) lines for writing.

Therefore, if a button is pressed next to the second input, the result must be stored in the second input, etc.

The code looks like this.

my $count = 0;
my @name = ();
my @val = ();
my @edit = ();
my @button = ();

my $fr = $wind->Frame->pack;

foreach ( @outputs ) {  
    $name[$count] = helper::trim($_);
    $val[$count] = '';

    $fr->Label(-text => $name[$count])->grid(-row => $count, -column => 0, -sticky => 'w');
    $edit[$count] = $fr->Entry(-textvariable => \$val[$count], -width => 30)->grid(-row => $count, -column => 1);
    $button[$count] = $fr->Button(-width => 20, -text => 'Select', 
            -command => sub { &select(\$val[$count], \$edit[$count]); } 
        )->grid(-row => $count, -column => 2);

    $count++;
} 

      

However, since & select is called after the complete dialog has been built, the counter is always at the last value and I don't get any information about which button I pressed. Strings like

-command => sub { &select(\@val, \@edit, $count); } 

      

doesn't work either.

Is there a way to get the widget that called the subroutine (so I can store the information I need in some way in the appropriate button) or use the actual value (from $ count here) in such a call?

Sincerely.

+3


source to share


1 answer


inside the callback:



my $caller = $Tk::widget;

      

+4


source







All Articles