Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Checkbutton in a loop

by gocpon (Novice)
on Apr 29, 2012 at 05:03 UTC ( #967899=perlquestion: print w/ replies, xml ) Need Help??
gocpon has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I create a checkbutton in loop, If I check any of the checkbutton i am not getting -text of the corresponding checkbutton .Kindly help me

#! c:\perl\Bin use Tk; use Tk::Pane; use Tk::CheckButton; use Tk::LabFrame; @a=qw/one two three four five six seven eight nine ten/; $mw = MainWindow->new; $mw->geometry('200x200'); $f = $mw->Scrolled("Frame",-borderwidth => 3, -relief => 'groove',-hei +ght=>20)->pack; #$scrollbar = $f->Scrollbar(-orient => 'vertical'); $j=0; foreach $i(1...20) { $c=$f->Checkbutton(-text=>$a[$j],-command=>\&fun)->pack(); $i++; $j++; } MainLoop; sub fun { print ("$a[$j]"); }

Comment on Checkbutton in a loop
Download Code
Replies are listed 'Best First'.
Re: Checkbutton in a loop
by Eliya (Vicar) on Apr 29, 2012 at 08:37 UTC

    What do you expect $j to be when the callback function gets called?  You have to somehow pass it as an argument to the routine.  There are two ways to pass the value:

    Either specify an array(ref) to the -command option

    ... for my $i (0...$#a) { $f->Checkbutton(-text => $a[$i], -command => [ \&fun, $i ] )->pack +(); } MainLoop; sub fun { my $j = shift; print $a[$j]; }

    Or use a closure

    ... for my $i (0...$#a) { $f->Checkbutton(-text => $a[$i], -command => sub { fun($i) } )->pa +ck(); } ...

      There is a third option, don't pass it any arguments

      use Tk; $mw = tkinit; for(1..4){ $mw->Checkbutton( -text => qq/check it $_/, -command => \&f, ) ->pack; } MainLoop; sub f { my $b = $Tk::event->W; warn join q/ /, $b->{Value}, $b->cget(q/-text/); } __END__ 1 check it 3 at - line 14. 0 check it 3 at - line 14. 1 check it 3 at - line 14. 0 check it 3 at - line 14. 1 check it 3 at - line 14. 0 check it 3 at - line 14. 1 check it 3 at - line 14. 0 check it 3 at - line 14.
Re: Checkbutton in a loop
by zentara (Archbishop) on Apr 29, 2012 at 13:30 UTC
    Others have given good answers to your question, but neglected to chastise you for your lousy code. So just in case this is homework, I offer these comments.:-)

    First off, if you added

    use warnings; use strict;
    you would have received a fatal error that there is no Tk::CheckButton module. Fix that error, then you load Tk::LabFrame but never use it. Instead you use Tk::Pane to make a scrolled frame, which has its packing options unset, so the whole checkbutton list is crushed to 1 entry. You even use the .. operator incorrectly, but luckily it seems to work.
    foreach $i (1...20) # that ... should be ..
    Your @a array only has 10 elements one to ten, but you make 20 checkbuttons.

    If you are a student, I wouldn't even accept this as a valid submission. Here is some unflawed code for you to look at.

    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Pane; my @a=qw/one two three four five six seven eight nine ten/; my $mw = MainWindow->new; $mw->geometry('200x200'); my $f = $mw->Scrolled("Frame",-borderwidth => 3, -relief => 'groove',- +height=>20) ->pack(-expand=> 1, -fill=>'both'); foreach my $i (0 .. 19){ # this should be foreach my $i (@a), but it' +s your code :-) print "$i\n"; my $c = $f->Checkbutton(-text=>$a[$i],-command=>[ \&fun, $a[$i] ] )->p +ack(); $i++; } MainLoop; sub fun{ print "@_\n"; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://967899]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2015-07-08 01:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (93 votes), past polls