Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Aligning Tk Checkboxes

by roho (Monsignor)
on Sep 13, 2012 at 01:38 UTC ( #993337=perlquestion: print w/ replies, xml ) Need Help??
roho has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to left align 4 sets of checkboxes in Tk. I have extracted the part of my program that builds the checkboxes. I am packing them to the left and anchoring them west, but the checkboxes are anything but aligned. I tried creating a frame for each of the 4 sets of checkboxes, but that did not help. Please note that I am building the sets of checkboxes from the bottom of the screen up, which should not matter as far as alignment goes. I'm sure that I'm missing somehting very simple here. How can I get the checkboxes to align regardless of the text that follows them? Thanks.

#!/usr/bin/perl use strict; use warnings; use Tk; my $mw = new MainWindow; $mw->geometry("360x250+280+60"); $mw->bind('<Escape>' => sub {exit;}); $mw->configure(-title => 'Test Checkbox Alignment'); my ($qv, $dg, $yb, $pv, $rd, $oc, $hr, $tm); ################################################################### # Create frame for 4th set of check boxes. ################################################################### my $frame_cb0 = $mw->Frame; $frame_cb0->pack(-side => "bottom", -pady => 1); ################################################################### # Create first check box for 4th set. ################################################################### my $qv_cbox = $frame_cb0->Checkbutton( -text => 'QuickCheck', -variable => \$qv, ); $qv_cbox->pack( -side => "left", -anchor => 'w', ); ################################################################### # Create second check box for 4th set. ################################################################### my $dg_cbox = $frame_cb0->Checkbutton( -text => 'Systems Guide', -variable => \$dg, ); $dg_cbox->pack( -side => "left", -anchor => 'w', ); ################################################################### # Create frame for 3rd set of check boxes. ################################################################### my $frame_cb1 = $mw->Frame; $frame_cb1->pack(-side => "bottom", -pady => 1); ################################################################### # Create first check box for 3rd set. ################################################################### my $yb_cbox = $frame_cb1->Checkbutton( -text => 'Study', -variable => \$yb, ); $yb_cbox->pack( -side => "left", -anchor => 'w', ); ################################################################### # Create second check box for 3rd set. ################################################################### my $pv_cbox = $frame_cb1->Checkbutton( -text => 'Analyze', -variable => \$pv, ); $pv_cbox->pack( -side => "left", -anchor => 'w', ); ################################################################### # Create frame for 2nd set of check boxes. ################################################################### my $frame_cb2 = $mw->Frame; $frame_cb2->pack(-side => "bottom", -pady => 1); ################################################################### # Create first check box for 2nd set. ################################################################### my $rd_cbox = $frame_cb2->Checkbutton( -text => 'Read', -variable => \$rd, ); $rd_cbox->pack( -side => "left", -anchor => 'w', ); ################################################################### # Create second check box for 2nd set. ################################################################### my $oc_cbox = $frame_cb2->Checkbutton( -text => 'Configure', -variable => \$oc, ); $oc_cbox->pack( -side => "left", -anchor => 'w', ); ################################################################### # Create frame for 1st set of check boxes. ################################################################### my $frame_cb3 = $mw->Frame; $frame_cb3->pack(-side => "bottom", -pady => 1); ################################################################### # Create first check box for 1st set. ################################################################### my $hr_cbox = $frame_cb3->Checkbutton( -text => 'Troubleshoot', -variable => \$hr, ); $hr_cbox->pack( -side => "left", -anchor => 'w', ); ################################################################### # Create second check box for 1st set. ################################################################### my $tm_cbox = $frame_cb3->Checkbutton( -text => 'Prioritize', -variable => \$tm, ); $tm_cbox->pack( -side => "left", -anchor => 'w', ); MainLoop; exit;

"Its not how hard you work, its how much you get done."

Comment on Aligning Tk Checkboxes
Download Code
Re: Aligning Tk Checkboxes
by protist (Monk) on Sep 13, 2012 at 02:26 UTC
    I'm guessing your checkboxes end up something like 1 1 --- 2 2 --- 3 3 --- 4 4 (dashes denote frame separation) If you want them to be aligned with any amount of text, you should use + two frames as follows: 1 | 1 2 | 2 3 | 3 4 | 4 This is using two frames each packed to the left. One frame contains the first check box for sets 1-4, the other contains the second check box for sets 1-4.

      G'day protist,

      On the basis of your textual description, that will produce the same dog's hind leg alignment that I described below.

      Perhaps you could post some code to clarify - maybe I've misunderstood your intent.

      -- Ken

        Probably this is what he meant:
        #!/usr/bin/perl use strict; use warnings; use Tk; my $mw = new MainWindow; $mw->geometry("360x250+280+60"); $mw->bind('<Escape>' => sub { $mw->destroy }); $mw->configure(-title => 'Test Checkbox Alignment'); # Main frame my $mf = $mw->Frame->pack( -side => 'bottom', -pady => 1 ); # Inner frames my $frm_l = $mf->Frame->pack( -side => 'left', -pady => 1 ); my $frm_r = $mf->Frame->pack( -side => 'right', -pady => 1 ); my $qv; my $qv_cbox = $frm_l->Checkbutton( -text => 'QuickCheck', -variable => \$qv, )->pack( -anchor => 'w', ); my $dg; my $dg_cbox = $frm_l->Checkbutton( -text => 'Systems Guide', -variable => \$dg, )->pack( -anchor => 'w', ); my $yb; my $yb_cbox = $frm_l->Checkbutton( -text => 'Study', -variable => \$yb, )->pack( -anchor => 'w', ); my $pv; my $pv_cbox = $frm_l->Checkbutton( -text => 'Analyze', -variable => \$pv, )->pack( -anchor => 'w', ); # Create 2nd set of check boxes. my $rd; my $rd_cbox = $frm_r->Checkbutton( -text => 'Read', -variable => \$rd, )->pack( -anchor => 'w', ); my $oc; my $oc_cbox = $frm_r->Checkbutton( -text => 'Configure', -variable => \$oc, )->pack( -anchor => 'w', ); my $hr; my $hr_cbox = $frm_r->Checkbutton( -text => 'Troubleshoot', -variable => \$hr, )->pack( -anchor => 'w', ); my $tm; my $tm_cbox = $frm_r->Checkbutton( -text => 'Prioritize', -variable => \$tm, )->pack( -anchor => 'w', ); MainLoop;

        Feel free to reorder the widgets how you need them to be.

        Regards, Ştefan

        P.S. It's also the way I would do the layout.

Re: Aligning Tk Checkboxes
by kcott (Abbot) on Sep 13, 2012 at 03:02 UTC

    G'day roho,

    To get the pairs of checkboxes aligned with the left border, you'll need to add -fill => 'x', e.g.

    $frame_cb0->pack(-side => "bottom", -pady => 1, -fill => 'x');

    However, that only aligns the first checkbox in each pair; the second checkboxes will have dog's hind leg alignment:

    [ ] Troubleshoot [ ] Prioritize [ ] Read [ ] Configure [ ] Study [ ] Analyse [ ] QuickCheck [ ] Systems Guide

    To align all boxes in a tabular fashion, I'd use Tk::grid instead of Tk::pack.

    [Hint: to see what parts of the overall window are covered by particular widgets, give them a background colour which stands out, e.g. Frame(-bg => 'red', ...), Checkbutton(-bg => 'blue', ...), etc.]

    -- Ken

Re: Aligning Tk Checkboxes
by zentara (Archbishop) on Sep 13, 2012 at 10:56 UTC
      Thanks for the suggestion. I installed the module, ran the code in the synopsis (after adding use TK; at the top and MainLoop; at the bottom), and got a blank Tk screen. Unfortunately the documentation is quite sparse. Do you happen to have a working sample of this module? It appears to be exactly what I need. Thanks.

      "Its not how hard you work, its how much you get done."

        #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::CheckbuttonGroup; my $top = MainWindow->new(); $top->geometry('400x400'); my @selected = qw(two four); my $checkbuttongroup = $top->CheckbuttonGroup ( -font => 24, -list => [qw( one two three four five )], -orientation => 'vertical', -variable => \@selected, -command => sub { print @selected, "\n"; } )->pack(); my @selected1 = qw(8 10); my $checkbuttongroup1 = $top->CheckbuttonGroup ( -font => 24, -list => [qw( 6 7 8 9 10 )], -orientation => 'horizontal', -variable => \@selected1, -command => sub { print @selected1, "\n"; } )->pack(); MainLoop;

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (11)
As of 2014-07-14 08:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (257 votes), past polls