0

I'm creating a list of checkbuttons in perl tk using a loop.

    $i=1;
    $n=5;
    @x=1;
    while($i <= $n){
    $mw->Checkbutton(->text=>$i,-variable=>\$ckval,->command=>sub{
                 if($ckval){print $i}
    })->pack;
    $i=$i+@x;
    }

The correct value of i is printed on the screen, but all the checkbuttons seem to be selected. How do I avoid this? Thanks in advance.

g3nair
  • 137
  • 5

2 Answers2

0

All checkbuttons are sharing the same variable $ckval. Maybe you want a radiobutton instead?

Slaven Rezic
  • 4,571
  • 14
  • 12
0

@Slaven is right: you are using the same variable. You could use a hash table or an array instead.

my $i=1;
my $n=5;
my $incr=1;
my @ckval;
while($i <= $n){
    $mw->Checkbutton(-text=>$i,-variable=>\$ckval[$i],->command=>sub{
             if($ckval[$i]){ print $i; }
    })->pack;
    $i=$i+$incr;
}
Jean-Francois T.
  • 11,549
  • 7
  • 68
  • 107