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

Fellow monks,
I have a question that is plaguing me to no end. I'm writing a TK app for a homework assingment and it has 6 checkboxes at the bottom.

What I would LIKE to do is to have them split in to two rows, so that I have the three on top, and three on the bottom.

What I have been able to achieve so far is:
1. Horizontal placement of all 6 - which means they're cut off unless I REALLY stretch the window. 2. Vertically stacked - but I can't get them to justify.

Ideally I want the two rows with three each but I'd even settle for vertical stacking at this point if I could get them to not center justify. I've tried using the option -justify=>'left' but it seems to ignore it when I run it.

Any ideas?

TIA!

Replies are listed 'Best First'.
Re: Lining up check boxes in Tk
by ariels (Curate) on Apr 21, 2002 at 07:48 UTC

    One way to arrange things in 2 rows of 3 each is to divide your area into 2 Tk::Frames:

    Red  Green  Blue <-- Top frame
    Mustard  Vinegar  Salt <-- Bottom frame
    Then pack each row into its frame, left-to-right:
    my $mw = new Tk::MainWindow; my $top = $mw->Frame->pack; my $bottom = $mw->Frame->pack; my @cb = ((map { $top->Checkbutton(-text => $_)->pack(-side => 'left') + } qw{Red Green Blue}), (map { $bottom->Checkbutton(-text => $_)->pack(-side => 'left') +} qw{Mustard Vinegar Salt})); MainLoop;

    Of course, this keeps the widths of different buttons independent. If you want everything nicely gridded out, use the Tk::grid geometry manager instead of Tk::pack. This lets you align everything, a bit like this:

    RedGreenBlue
    MustardVinegarSalt
    You'll probably want to stick each Checkbutton to the east side of its box, too:
    my $mw = new Tk::MainWindow; my @cb = ((map { $mw->Checkbutton(-text => (qw{Red Green Blue})[$_])-> grid(-column => $_, -row => 0, -sticky => 'w') } 0..2), (map { $mw->Checkbutton(-text => (qw{Mustard Vinegar Salt})[$_]) +-> grid(-column => $_, -row => 1, -sticky => 'w') } 0..2)); MainLoop;


    ariels -- Add table to explicate the thing with the grid.
Re: Lining up check boxes in Tk
by {NULE} (Hermit) on Apr 21, 2002 at 16:03 UTC
    Hey Popcorn Dave,

    ariels response sums up most the options when it comes to Tk Geometry management. To see all of the options check out the main documentation for Tk (perldoc Tk from your command-line on most platforms) and check the section called "Tk Geometry Management".

    In my experience Tk::grid and Tk::Table are the easiest to use and best documented. Tk::Table has the advantage over grid in some circumstances because it is already in a scrolled frame (actually it ISA scrolled frame :) ). That may or may not be of use to you.

    Good luck,
    {NULE}
    --
    http://www.nule.org