Blurman
Blurman

Reputation: 609

PERL tk module handling

I am using tk module to build a checklist for user to choose. I am totally beginner to this module. Currently the checklist created is all in the same series column, since it will be a long list, any idea how could I separate few of the options to another column? Also, any idea how to align those text to the left while checkbox to the right? To display it cleanly and neatly.

#!/usr/bin/perl
use Tk;


$main = MainWindow->new();
$label = $main->Label(-text => "Presence Check");

$label->pack();
$frame = $main->Frame(-relief=>"groove", -borderwidth=>2);
#$frame = $main->Frame(-relief=>"groove", -borderwidth=>2)->pack(-side => 'top', -expand => 1, -fill =>'both');
#$frame = $main->Frame->pack(-side => 'left', -fill => 'x');



$check1 = $frame->Checkbutton(-text=>"Document A (docx, pdf)",
                          -variable=>\$a,
                  -onvalue=>"APRESENT",
                  -offvalue=>"AABSENT");

#$check1->pack(-side=>"top");
$check2 = $frame->Checkbutton(-text=>"Document B (xlsx)",
                          -variable=>\$b,
                  -onvalue=>"BPRESENT",
                  -offvalue=>"BABSENT");

$check2->pack(-side=>"top");

$check3 = $frame->Checkbutton(-text=>"C specification",
                          -variable=>\$c,
                  -onvalue=>"CPRESENT",
                  -offvalue=>"CABSENT");

$check3->pack(-side=>"top");

$check4 = $frame->Checkbutton(-text=>"A-Specification",
                          -variable=>\$aspec,
                  -onvalue=>"ASPECPRESENT",
                  -offvalue=>"ASPECSABSENT");

$check4->pack(-side=>"top");

$check5 = $frame->Checkbutton(-text=>"Important Report",
                          -variable=>\$report,
                  -onvalue=>"REPORTPRESENT",
                  -offvalue=>"REPORTSABSENT");

$check5->pack(-side=>"top");

$check6 = $frame->Checkbutton(-text=>"Handbook",
                          -variable=>\$handbook,
                  -onvalue=>"HANDBOOKPRESENT",
                  -offvalue=>"HANDBOOKABSENT");

$check6->pack(-side=>"top");

$check7 = $frame->Checkbutton(-text=>"Data Spreadsheet",
                          -variable=>\$dataxls,
                  -onvalue=>"DATAPRESENT",
                  -offvalue=>"DATAABSENT");

$check7->pack(-side=>"top");

$check8 = $frame->Checkbutton(-text=>"D file",
                          -variable=>\$dfile,
                  -onvalue=>"DFILEPRESENT",
                  -offvalue=>"DFILEABSENT");

$check8->pack(-side=>"top");


$check10 = $frame->Checkbutton(-text=>"xx doc",
                          -variable=>\$xxdoc,
                  -onvalue=>"XXDOCPRESENT",
                  -offvalue=>"XXDOCABSENT");

$check10->pack(-side=>"top");

$check18 = $frame->Checkbutton(-text=>"yy Doc",
                          -variable=>\$yydoc,
                  -onvalue=>"YYDOCPRESENT",
                  -offvalue=>"YYDOCABSENT");

$check18->pack(-side=>"top");
$frame->pack();

$button = $main->Button(-text => "Exit",
                        -command => \&exit_button);
$button->pack();
MainLoop();

sub exit_button {
print "$a $b $c $aspec $report $handbook $dataxls $dfile $xxdoc $yydoc \n";
#print "$rv\n";
exit,
}

Upvotes: 1

Views: 132

Answers (2)

Polar Bear
Polar Bear

Reputation: 6798

Please see attached screenshot to this code snippet if it is what you desired

!/usr/bin/perl
#
# vim: ai:ts=4:sw=4
#

use strict;
use warnings;
use feature 'say';

use Tk;
use Data::Dumper;


my $main    = MainWindow->new( -title => 'Checkboxes' );
my $label   = $main->Label(-text => 'Presence Check');

$label->pack();

my $frame = $main->Frame(-relief=>'groove', -borderwidth=>2);

my $values;
my @chk_boxes = (
        [\$values->{doc_a},'Document A (docx, pdf)'],
        [\$values->{doc_b},'Document B (xlsx)'],
        [\$values->{spec_c},'C specification'],
        [\$values->{spec_a},'A-Specification'],
        [\$values->{report},'Important Report'],
        [\$values->{hand_b},'Handbook'],
        [\$values->{data_s},'Data Spreadsheet'],
        [\$values->{data_d},'D file'],
        [\$values->{doc_xx},'xx doc'],
        [\$values->{doc_yy},'yy Doc']
    );

check_box(\@chk_boxes);

$frame->pack();

my $button = $main->Button(
                -text => 'Exit',
                -command => \&exit_button
            );

$button->pack();

MainLoop();

sub exit_button {
    while( my($k,$v) = each %{$values} ) {
        say "$k => $v" if $v;
    }
    exit;
}

sub check_box {
    my $data = shift;

    my $index = 0;

    for my $chk_box ( @{$data} ) {
        my $column = $index % 2;
        my $row    = int($index/2);
        $frame->Checkbutton(
                -text       => $chk_box->[1],
                -variable   => $chk_box->[0]
                )->grid( -column => $column, -row => $row, -sticky => 'w');
        $index++;
    }
}

Perhaps following form of data input may be preferable

#!/usr/bin/perl
#
# vim: ai:ts=4:sw=4
#

use strict;
use warnings;
use feature 'say';

use Tk;
use Data::Dumper;


my $main    = MainWindow->new( -title => 'Checkboxes' );
my $label   = $main->Label(-text => 'Presence Check');

$label->pack();

my $frame = $main->Frame(-relief=>'groove', -borderwidth=>2);

my $values;
my @chk_boxes;

while( <DATA> ) {
    chomp;
    my($var,$desc) = split '\t';
    push @chk_boxes, [\$values->{$var}, $desc];
}

check_box(\@chk_boxes);

$frame->pack();

my $button = $main->Button(
                -text => 'Exit',
                -command => \&exit_button
            );

$button->pack();

MainLoop();

sub exit_button {
    while( my($k,$v) = each %{$values} ) {
        say "$k => $v" if $v;
    }
    exit;
}

sub check_box {
    my $data = shift;

    my $index = 0;

    for my $chk_box ( @{$data} ) {
        my $column = $index % 2;
        my $row    = int($index/2);
        $frame->Checkbutton(
                -text       => $chk_box->[1],
                -variable   => $chk_box->[0]
                )->grid( -column => $column, -row => $row, -sticky => 'w');
        $index++;
    }
}

__DATA__
doc_a   Document A (docx, pdf)
doc_b   Document B (xlsx)
spec_c  C specification
spec_a  A-Specification
report  Important Report
hand_b  Handbook
data_s  Data Spreadsheet
data_d  D file
doc_xx  xx Doc
doc_yy  yy Doc

Add-on:

sub check_box {
    my $data = shift;

    my $index = 0;

    for my $chk_box ( @{$data} ) {
        my $column = $index % 2;
        my $row    = int($index/2);
        my $chk_x = $frame->Checkbutton(
                -text       => $chk_box->[1],
                -variable   => $chk_box->[0]
                )->grid( -column => $column, -row => $row, -sticky => 'w');
        $chk_x->select if $index == 4;
        $index++;
    }
}

screenshot

Upvotes: 0

H&#229;kon H&#230;gland
H&#229;kon H&#230;gland

Reputation: 40718

... any idea how could I separate few of the options to another column? Also, any idea how to align those text to the left while checkbox to the right?

Here is an example of how you can use the grid geometry manager to align the labels and checkboxes:

use feature qw(say);
use strict;
use warnings;
use Tk;

my $mw = MainWindow->new();
my @label_texts = (
    "Document A (docx, pdf)",
    "Document B (xlsx)",
    "C specification"
);

my @labels;
my @checkvars;
my @check_buttons;
for my $text ( @label_texts ) {
    my $label = $mw->Label(-text => $text);
    push @labels, \$label;
    my $check_var;
    my $check = $mw->Checkbutton(
        -text=>"",
        -variable=>\$check_var,
    );
    push @checkvars, \$check_var;
    push @check_buttons, \$check;
    Tk::grid($label, $check, -sticky => 'w');
}

my $button = $mw->Button(
    -text => "Exit",
    -command => \&exit_button
);
Tk::grid($button, "-");

MainLoop();

sub exit_button {
    for my $i (0..$#checkvars) {
        my $var = $checkvars[$i];
        my $state = $$var;
        $state = 'undef' if !defined $state;
        say "Variable $i: ", $state;
    }
    exit;
}

enter image description here

Upvotes: 1

Related Questions