in reply to Perl/TK get the Button's text

Hi,

here is an example, you can either use the widgets ref and call cget on it or store the text instead.

#!/usr/bin/perl use Tk; use strict; use warnings; my $mw = tkinit; my @buttons; for my $i(0..5){ push @buttons, $mw->Button(-text => $i, -command => sub{ print "pressed $i\n"; # or use the widget: print "pressed ", $buttons[$i]->cget('-text'), "\n"; })->pack; } MainLoop();
Cheers, Christoph

Replies are listed 'Best First'.
Re^2: Perl/TK get the Button's text
by Anonymous Monk on Jun 03, 2009 at 16:47 UTC
    But -text is part of an array (@row $row$i) and not not the $i only how can i get the data from an array ($row$i) )
      Hi,

      this was only meant to be an example on how the Button options can be used. In real world I'd keep data seperated from widgets wherever possible. Also if your data is dynamic I would prefer a Listbox or something similar over dynamically created buttons. That's what most people are used to. But this might well be my very personal taste ;)

      #!/usr/bin/perl use Tk; use strict; use warnings; my $mw = tkinit; my @buttons; my @data = qw/foo bar baz this that whatever /; for my $i(0..5){ push @buttons, $mw->Button(-text => $data[$i], -command => sub{ print "pressed $data[$i]\n"; print "pressed ", $buttons[$i]->cget('-text'), "\n"; })->pack; } MainLoop();
      Cheers, Christoph