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

This seems to work.
use strict; #use warnings; #use diagnostics; use Tk; my $mw = MainWindow->new; $mw->title( 'Albums' ); open my $FILE, "<Album_Names", or die "Can't open file: $!"; my @lines = <$FILE>; my $NumberOfAlbums = scalar @lines; print "You have $NumberOfAlbums albums.\n"; close( $FILE ) or die "Cannot close file: $!"; my $i = 0; my @Selected; foreach my $n ( @lines ) { $mw->Checkbutton( -text => "$n", -onvalue => $n, -offvalue => 0, -variable => \$Selected[$i], )->pack(-side => 'top', -anc +hor => 'nw' ); $i=$i+1; } my $GetAlbumsButtons = $mw->Button( -text => "Get Albums", -command => \&value, )->pack( -side => 'bottom', +-anchor => 's' ); sub value { for my $x ( @Selected ) { ## Regex is a test to see if it works. if( $x ne 0 && $x =~ m/LINUX/ ) { print "$x"; } } print "\n"; }; MainLoop;
#########################################################################
How can I print the values of the check buttons selected?
use strict; use warnings; use Tk; my $mw = MainWindow->new; $mw->title( 'Albums' ); my @Buttons; open my $FILE, "<Album_Names", or die "Can't open file: $!"; my @lines = <$FILE>; my $NumberOfAlbums = scalar @lines; print "You have $NumberOfAlbums albums.\n"; close( $FILE ) or die "Cannot close file: $!"; my $i = 0; my @Selected; foreach my $n ( @lines ) { push( my @Values, $mw->Checkbutton( -text => "$n", -variable => \$Selected[$i], )->pack(-side => ' +top', -anchor => 'nw' ) ); $i=$i+1; } my $GetAlbumsButtons = $mw->Button( -text => "Get Albums", -command => sub { print "Get Albums Button Pressed +.\n"; }, )->pack( -side => 'bottom', -anchor => 's' ); MainLoop;


The file Album_Names contains the following lines =>
Mobile Uploads Timeline Photos Untitled Album LINUX Cover Photos 5k Races 2013 Dirt bike Races 2013 Profile Pictures

Replies are listed 'Best First'.
Re: print the values of the selected check buttons
by zentara (Cardinal) on Jan 12, 2014 at 13:16 UTC
    Something like this standard code example should work:
    #!/usr/bin/perl use strict; use Tk; # @checkboxes is an array of two-element arrays, each of which # provides a label in element 0 and a value in element 1. # A reference to element 1 is given to the -variable option # of Tk::Checkbutton. my @checkboxes = ( ['First Checkbox' => 0], ['Second Checkbox' => 0], ['Third Checkbox' => 0], ); my $mw = tkinit; MakeCBs($mw, 'Label:', @checkboxes); $mw->Button( -text => 'Print checkbox values', -command => sub { foreach (@checkboxes) {printf "%s: %s\n", $$_[0], $$_[1]} }, )->pack; MainLoop; sub MakeCBs { my ($w, $label, @cbs) = @_; $w->Label(-text => $label)->pack(-side => 'left'); foreach (@cbs) { $w->Checkbutton( -text => $$_[0], -variable => \$$_[1], )->pack(-anchor => 'w'); } }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: print the values of the selected check buttons
by Anonymous Monk on Jan 12, 2014 at 04:17 UTC

    What does the documentation say?

    What do the examples say?

    What is @Buttons used for?

    What does  push( my @Values, do? What is inside of @Values? What is @Values used for? What is the purpose of the my?

    What are is inside of  @Selected;?

    If you can answer these questions you can have your answer

    #!/usr/bin/perl -- ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END" -otr -opr -ce +-nibc -i=4 -pt=0 "-nsak=*" use strict; use warnings; use Path::Tiny qw/ path /; use Data::Dump qw/ dd pp /; use Tk; Main( @ARGV ); exit( 0 ); sub Main { my( $file ) = @_; my @lines = ( $file ? path( $file )->lines_raw : 1 .. 4 ); s/\s+$// for @lines; GoTk( \@lines ); } sub GoTk { my( $albums ) = @_; my $mw = MainWindow->new; $mw->title( scalar( @$albums ) . ' Albums ' ); my @buttons; for my $album ( @$albums ) { push @buttons, $mw->Checkbutton( -text => "$album", -onvalue => 1, -offvalue => 0, )->pack( -side => 'top', -anchor => 'nw' ); } my $GetAlbumsButtons = $mw->Button( -text => "Get Albums", -command => [ \&get_albums, $mw, \@buttons, ], )->pack( -side => 'bottom', -anchor => 's' ); #~ use Tk::WidgetDump; $mw->WidgetDump; MainLoop; } ## end sub GoTk sub get_albums { my( $mw, $buttons ) = @_; #~ my @buttons = grep { $_->isa("Tk::Checkbutton") } $mw->children + ; my $pp = ""; for my $but ( @$buttons ) { my $var = $but->cget( -variable ); my $text = $but->cget( -text ); my $val = $$var; #~ my $val = $but->{Value}; dd( { checked => $val, text => $text } ); $pp .= "\n" . pp( { checked => $val, text => $text } ); } $mw->MsgBox( -title => "Selected Albums", -message => $pp, -type => "okcancel", )->Show; } ## end sub get_albums __END__