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

Hi All, this below sub creates buttons by order using an foreach loop. and adds text to them using an arry (@row$i) my question is: i want to click on the button and print the text that appers on that specific button. any idea how ?
sub copy_entry { $i = 0; my %hash; $search = $entrysearch->get(); $f1 = $mwf->Frame()->pack(-expand => 0,-fill => 'both' ); foreach $fr($f1) { my $i =0; foreach (1..5) { $hash{'text'} = $fr->Button( -relief => 'groove', -background => 'white', -foreground => 'black', -font => "Ariel 8 bold", -text => $row[$i], -height => 1, -width => 1 )->pack(-side=>'left',-expand => 1, + -fill => 'both' ); $i++; } } }

Replies are listed 'Best First'.
Re: Perl/TK get the Button's text
by lamprecht (Friar) on Jun 03, 2009 at 13:12 UTC
    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
      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
Re: Perl/TK get the Button's text
by liverpole (Monsignor) on Jun 03, 2009 at 18:17 UTC
    Hi Aboveyou,

    (Not to be confused with "high aboveyou" :-))

    If you want to do this dynamically, so that it will work even after the button's text has changed, you can either use the -textvar option suggested above, or you can construct the button in two steps.

    For example, the first step would be something like:

    my $button = $mw->Button(-text => $label);
    and in the second step, a subroutine (which takes the button name as an argument) is configured as the command invoked when the button is pressed:

    $button->configure(-command => [ \&show_button_label, $button ]); -or- $button->configure(-command => sub { show_button_label($button) }) +;

    Obviously you can't do this in one step, as $button isn't defined until you have the return value from $mw->Button, and you need to pass $button to the subroutine.

    Here's a full example:

    #!/usr/bin/perl # Libraries use strict; use warnings; use Tk; # User-defined my @button_labels = (qw( Red Blue Green White Yellow Orange )); # Main program my $mw = new MainWindow(); foreach my $label (@button_labels) { # Note labels are colors, so they can be used for the background my $button = $mw->Button(-text => $label, -bg => $label); $button->configure(-command => [ \&show_button_label, $button ]); $button->pack(-side => "left"); } MainLoop(); # Subroutines sub show_button_label { my ($button) = @_; my $label = $button->cget(-text); print "Label for button '$button' is '$label'\n"; }

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Perl/TK get the Button's text
by Anonymous Monk on Jun 03, 2009 at 09:20 UTC
    Maybe instead of using the -text option you could use the -textvariable option and print the content of the variable when the button is clicked
Re: Perl/TK get the Button's text
by Anonymous Monk on Jun 03, 2009 at 08:49 UTC
    | V my $text = $text->cget( -text ); ^ |
    where does $text come from?
      Sorry please ignore that lineit was something i tried (didnt work)
Re: Perl/TK get the Button's text
by hangon (Deacon) on Jun 03, 2009 at 17:11 UTC

    Or you could use the -command option to pass arguments to a named subroutine. TMTOWTDI. Also note how $i is used as the loop variable.

    ... my @buttons; for my $i (0..5){ push @buttons, $mw->Button( -text => $row[$i], -command => [ \&buttonpress, $row[$i] ], )->pack; } sub buttonpress{ my $btn = shift; print "button $btn pressed\n"; }