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

Hello Monks, I am trying to learn Tk module. I want to have a checkbutton for each US state and upon submit i want to know what checkbuttons have been checked. I was able to get the data entered from the textbox(Entry) but not the checkbuttons. Any help is really appreciated. Thank you Akhila Below is my code approach:
#! /usr/bin/perl -w use Tk; my $mw = new MainWindow; $mw->geometry("300x300"); $mw->title("Perl Tk Checkbutton"); my $check_frame = $mw->Frame()->pack(-side=>"top"); my $label=$check_frame->Label(-text=>"Enter keywords")->pack(-side=>"t +op")->pack(); my $entry = $check_frame->Entry()->pack(); #my $chk1 = $check_frame->Checkbutton(-text=>"Alaska",-variable=>"Alas +ka")->pack(); my $state = "Alaska"; my $chk1 = $check_frame->Checkbutton(-text=>"Alaska",-command=>[\&chec +k_sub,$state])->pack(); my $chk2 = $check_frame->Checkbutton(-text=>"Michigan",-variable=>"Mic +higan")->pack(); my $button_frame = $mw->Frame()->pack(-side=>"bottom"); my $ok_button = $button_frame->Button(-text=>"Submit",-command=>\&chec +k_sub)->pack(-side=>"left"); MainLoop; sub check_sub{ print $_; my @names = $entry->get(); print @names; exit; }

Replies are listed 'Best First'.
Re: perl tk Check button.
by zentara (Cardinal) on Apr 12, 2012 at 15:18 UTC
    Here is a way to do it. There may be a better way, but this is pretty foolproof.
    #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Pane; my @states = qw(Alaska Hawaii Michigan Georgia Arizona Washington Oreg +on California Texas Utah Minnesota); my $mw = tkinit(); $mw->geometry('300x300+100+100'); my $sp = $mw->Scrolled('Pane', -scrollbars=>'osoe', sticky=>'nwse') ->pack(-expand=>1, -fill=>'both' ); my @cbvalues; my @cbnames; my $count = 0; foreach my $d( @states){ $cbnames[$count] = $d; $sp->Checkbutton(-text => $d, -font=>[arial => 12], -onvalue => 1, -offvalue => 0, -variable => \$cbvalues[$count], -font => 'big', -bg => 'white', )->pack(-anchor=>'w')->pack(); $count++; } my $showbutton= $mw->Button(-text=>'Show Selected', -bg => 'lightyellow', -command => sub{ my @selected = (); foreach my $c( 0.. $count ){ if ( $cbvalues[$c] ){ push @selected, $cbnames[$c]; } } print "@selected\n"; } )->pack(); MainLoop();

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Wow that was perfect. Can you refer any tutorial that i can follow for TK.
        There are many on google, but a few good ones are Tk tutorial, and uwinnipeg Tk tutorial. Also, there are 2 books available online, the oldest is Learning Perl/Tk, and the best is Mastering Perl/Tk. You can find sample chapters online, and the "Perl CD Bookshelf Version 3.0" ( an older version of the bookshelf) has 6 Perl books on it including Mastering Perl/Tk. A quick look at Ebay, has one for $22.50, pretty good for 6 Perl books. I don't know the legality of this site, but look at Mastering Perl/Tk

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