If I'm following you correctly, what you want is something like this:
#!/usr/bin/perl -w use strict; use warnings; use Tk; # User-defined my @button_names = qw( blue red green white purple orange ); # Main program my $mw = new MainWindow(-title => 'Button example'); my $fr = $mw->Frame()->pack(-fill => 'x'); foreach my $name (@button_names) { my $b = $fr->Button(-text => $name, -bg => $name)->pack(-side => ' +left'); $b->configure(-command => sub { invoke_button($b, $name) }); } MainLoop; + # Subroutines sub invoke_button { my ($button, $name) = @_; print "You clicked on button '$name'; object is '$button'\n"; }
The trick is that you create the button first, and then configure it to use a callback which is passed a reference to the button itself.
In reply to Re: a Tk object who knows who he is ?
by liverpole
in thread a Tk object who knows who he is ?
by spx2
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |