Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Array of TK::Checkbuttons not acting appropriately

by mikasue (Friar)
on Oct 02, 2006 at 12:33 UTC ( #575864=note: print w/ replies, xml ) Need Help??


in reply to Array of TK::Checkbuttons not acting appropriately

Thanks for your help. I have been trying to figure this out all weekend and got a little frustrated.

Forgive me everyone.

Here is a little snippet that everyone can run to see my problem. The problem again is when I click 1 checkbutton they all get selected.

use strict; use warnings; use Tk; use Tk::Table; our $MW = MainWindow->new(%{$BPP::BPPDEFAULTS::MYDEFAULTS{MAINWINDOW}} +); $MW->geometry('800x700+0+0'); my $tableframe = $MW->Frame(-background=>'white' )->pack(-side=>'top', -fill=>'both', -pady=>20); my $bill_table = $tableframe->Table(-rows=>2, -columns=>3, -background=>'pink', -scrollbars=>'e' )->pack(-fill=>'both', -side=>'top', -anchor=>'s'); my @foo = qw/ Apple Oranges Pears/; my $x = 1; my $checkbox; my $checkboxvalue = 0; foreach my $f (@foo) { $checkbox = $bill_table->Checkbutton(-variable=>\$checkboxvalue); $bill_table->put($x,0, $checkbox); $bill_table->put($x,1, $f); $bill_table->Button(-text=>'Submit', -command=>sub{print $f} )->pack; $x++; }#end foreach loop MainLoop;

So to distinguish them I tried putting the -variable value in an array. This fixes the selection issue but then that -variable value is not being set when I click on the checkbutton. I get this error: Use of uninitialized value in numeric eq (==)

use strict; use warnings; use Tk; use Tk::Table; our $MW = MainWindow->new(%{$BPP::BPPDEFAULTS::MYDEFAULTS{MAINWINDOW}} +); $MW->geometry('800x700+0+0'); my $tableframe = $MW->Frame(-background=>'white' )->pack(-side=>'top', -fill=>'both', -pady=>20); my $bill_table = $tableframe->Table(-rows=>2, -columns=>3, -background=>'pink', -scrollbars=>'e' )->pack(-fill=>'both', -side=>'top', -anchor=>'s'); my @foo = qw/ Apple Oranges Pears/; my $x = 1; my $t=0; my $checkbox; my @checkboxvalue; foreach my $f (@foo) { $checkbox = $bill_table->Checkbutton(-variable=>\$checkboxvalue[$t]); $bill_table->put($x,0, $checkbox); $bill_table->put($x,1, $f); $bill_table->Button(-text=>'Submit', -command=>sub{if ($checkboxvalue[$t] ==1){prin +t $f}} )->pack; $x++; $t++; }#end foreach loop MainLoop;

I hope this makes my problem clearer.
================================================

I need a Checkbutton and a Submit button on each line becuase there will be distinct information I need to change on each line. When the Checkbutton is selected, the information in that line is editable and the Submit button submits those changes for that line only.

I also need a reference to the checkbutton in order to PUT it in the table.

$checkbox = $bill_table->Checkbutton(-variable=>\$checkboxvalue[$t]); $bill_table->put($x,0, $checkbox);

Thanks again for all ya'll help


Comment on Re: Array of TK::Checkbuttons not acting appropriately
Select or Download Code
Re^2: Array of TK::Checkbuttons not acting appropriately
by davidrw (Prior) on Oct 02, 2006 at 13:19 UTC
    The problem is with the closure (the anonymous sub{...}</> that -command is) and the scope of $t.  Put a <c>warn $t in the sub{} to see what it's doing -- you'll see that it's always 3 which is the value of t after the foreach my $f (@foo) ... Here are two different approaches to solving it:

    Here, you can use a temp value to hold the current value of $t. Since $i is scoped inside the for loop, it is different for each closure that is made.
    my $i=$t; $bill_table->Button(-text=>'Submit', -command=>sub{if ($checkboxvalue[$i] ==1){prin +t $f}}, )->pack;
    Alternatively, use the form of Tk::callback that lets you provide arguments to a sub. Here our anonymous sub takes two args -- a reference to a scalar which is the checkbox value, and a scalar which is the text of the checkbox.
    $bill_table->Button(-text=>'Submit', -command=>[ sub{ my $valref = shift; my $valname=shift; if ($$valref ==1){print $valname} }, \$checkboxvalue[$t], $f, ], )->pack;
    Now, there's the issue of your Use of uninitialized value in numeric eq (==) .. That is because the checkbox values are either 1 or undef, so when you check <thecheckboxvalue>==1 it'll do either 1==1 (ok) or undef==1 (warning).
    To solve, simply do one of these (probably the first? not sure that being exactly 1 is important, as long as it's true:
    if( $foo ){ print $f } if( defined $foo ){ print $f } if( $foo && $foo==1){ print $f } if( defined $foo && $foo==1){ print $f }
      THANK YOU davidrw!!!!
      That is because the checkbox values are either 1 or undef

      I'm a little puzzled. Checkbutton values are supposed to be 1 or 0, defaulting to 0, though I can't argue with what I'm seeing. Try selecting, then deselecting a Checkbutton using mikasue's last example. When you press the Submit button for that row now, you shouldn't see the "Use of unitialized..." message, because now the value is 0 instead of undef. I'm going to have to take another look at why my second and third examples didn't behave this way.

      Having said that, I agree that your first condition:  if ($foo) { ... } is best for this.

      Rob

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2015-07-07 02:56 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 (86 votes), past polls