Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Array of TK::Checkbuttons not acting appropriately

by rcseege (Pilgrim)
on Oct 02, 2006 at 02:01 UTC ( [id://575827]=note: print w/replies, xml ) Need Help??


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

Updated: Added code to example to include -command option for Checkbutton.

Updated Again: Created a second example based on my first, to try and more closely duplicate the scenario - I still don't see the same problem.

Why do you insist on making it difficult for people to help you? A small working example might have even shown you the solution before you posted the question. My guess is that you are setting the same variable for each Checkbutton because I don't see you incrementing t anywhere.

This example worked for me:

use strict; use Tk; my @cbValues; my $mw = MainWindow->new; $mw->Button( -text => "Print Selected", -command => \&printSelected )->pack; my $rows = $mw->Frame->pack; ## $rows will serve as a gridded container for each ## of the checkbutton rows. This should be roughly ## similar to using Tk::Table, but less hassle ## for a quick & dirty example. for my $row (0 .. 9) { $rows->Label(-text => $row)->grid( $rows->Checkbutton( -command => [\&select, $row], -variable => \$cbValues[$row])); } MainLoop; sub select { my $i = shift; if ($cbValues[$i]) { print "Selected $i\n"; } else { print "Deselected: $i\n"; } } sub printSelected { my $row = 0; print "\nSelected:\n"; foreach my $val (@cbValues) { print "$row selected\n" if $val; $row++; } }

Another example with a submit button for each row:

use Tk; use strict; my @cbValues; my $mw = MainWindow->new; $mw->Button( -text => "Print Selected", -command => \&printSelected )->pack; my $rows = $mw->Frame->pack; ## $rows will serve as a gridded container for each ## of the checkbutton rows. This should be roughly ## similar to using Tk::Table, but less hassle ## for a quick & dirty example. for my $row (0 .. 9) { $rows->Checkbutton( -text => $row, -variable => \$cbValues[$row] )->grid( $rows->Button( -text => "Submit", -command => sub { if ($cbValues[$row] == 1) { print "$row selected\n" } else { print "$row unselected\n"; } })); } MainLoop; sub printSelected { my $row = 0; print "\nSelected:\n"; foreach my $val (@cbValues) { print "$row selected\n" if $val; $row++; } }
Rob

Replies are listed 'Best First'.
Re^2: Array of TK::Checkbuttons not acting appropriately
by mikasue (Friar) on Oct 02, 2006 at 02:23 UTC
    Oh sorry.
    I do increment $t in my program. Didn't paste that line sorry.

    Why do you insist on making it difficult for people to help you? A small working example might have even shown you the solution before you posted the question.
    Why are you so mean?? I have a small program and that is not the solution - (the $t increment) I am doing that in my program.

    If you have to be condescending and mean just don't respond because it doesn't help me solve my problem.
    Thanks!

      rcseege wasn't being mean. He was trying to be helpful. He actually said much the same thing that I did and for the same reason - you have posted a chunk of code that we can't run and can't even analyse in any sensible fashion because it contains a pile of irrelevant code and some code that may be important, but which is clearly using objects that we know nothing about.

      The best we can do is guess at what your problem is, and Rob did correctly identify at least one problem with the code you posted.

      It is time to be humble, appologise to Rob for complaining about his help and think about how you might present your problem in a fashion such that we can actually help you with it.

      It may help you to read I know what I mean. Why don't you? when reformulating your question.


      DWIM is Perl's answer to Gödel

      I do apologize for coming across as mean or condescending -- that really was not my intent. I admit that I was a bit incredulous and slightly frustrated with the messy snippet you provided which wouldn't run and was littered with references to things defined elsewhere. You didn't even take the few moments required to clean things up a bit so that it would be more readable... I may be overstating things, but I found it borderline offensive, and also not very helpful in getting a resolution to your problem.

      Please try modifying one of the other examples provided so that they show the same error condition.

      Regards,

      Rob

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-16 05:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found