Commy_Code_Monkey has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I am looking for a way to identify a widget by clicking on it. ie Click on a button with return "$button1" or "1" or something similar. The tricky part is that the widgets are generated by the script itself. for example:
sub makewidget{ $counter=shift; ${widget."$counter"}=$mw->Button( -command=>sub{configurewidget($counter)} )->pack(); }
I can't seem to pass $count into the command to use as an identifier for this particular widget because it will change as $counter changes with time. I'm looking for a way to be able to identify which button was pressed. I've been trying to do it using x and y coordinates, but it has to be in a scrolling pane and it keeps screwing up my logic when the pane scrolls down. I'm relatively new to perl and Tk and I'm aware that using variables to define variables is poor practise at best. Thanks,

Replies are listed 'Best First'.
Re: Widget Information
by zentara (Cardinal) on Jan 23, 2009 at 20:09 UTC
    Here is some lowlevel magic, that gets the caller (currently active widget).
    #!/usr/bin/perl use strict; use Tk; use Data::Dumper; my $mw = MainWindow->new; for(0..4){ $mw->Button(-text => "Hello World$_", -command=>[\&change])->pack; } MainLoop; sub change { #Tk dosn't pass it's widget reference in callbacks #Only the bind() method will implicity pass a widget reference, and ev +en #this default (and generally preferred) action can be defeated. #use $Tk::widget print "sub-input->@_\n"; my $caller = $Tk::widget; #print Dumper([$caller]); print "$caller "; print $caller->{'_TkValue_'},' '; my $text = $caller->cget('-text'); print "$text\n"; $caller->configure(-text=>"Hello Stranger"); }

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      Brilliant! Thanks, -J