in reply to a Tk object who knows who he is ?

Hi spx2,

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.


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Replies are listed 'Best First'.
Re^2: a Tk object who knows who he is ?
by spx2 (Deacon) on Aug 24, 2007 at 10:17 UTC

    yes you're followin correctly. it's also as the approach I thought of...using a closure. thank you