in reply to Re: split hash into two columns
in thread split hash into two columns
#!/usr/local/bin/perl -w use Tk; use Grid; $mainWin = MainWindow->new(); $mainWin->resizable(0, 0); $mainWin->title("Fit columns and rows"); $rowsOrColumns = 1; $rowColFrame = $mainWin->Frame( -relief => "ridge", -borderwidth => 2)->pack( -side => "top", -fill => "y", -expand => 1); $rowColFrame->Label( -textvariable => \$rowColText, -relief => 'flat', -borderwidth => 2, -padx=> 5, -pady => 5)->pack( -side => 'top'); $chooseRowColFrame = $rowColFrame->Frame( -relief => "flat", -borderwidth => 2)->pack( -side => "top", -fill => "y", -expand => 1); $chooseRowColFrame->Radiobutton( -text => "Rows", -width => 7, -value => 0, -variable => \$rowsOrColumns, -command => \&arrangeButtons)->pack( -side => "left", -padx => 5, -pady => 5); $chooseRowColFrame->Radiobutton( -text => "Columns", -width => 7, -value => 1, -variable => \$rowsOrColumns, -command => \&arrangeButtons)->pack( -side => "left", -padx => 5, -pady => 5); $sortDirection = 0; $directionFrame = $mainWin->Frame( -relief => "ridge", -borderwidth => 2)->pack( -side => "top", -fill => "y", -expand => 1); $directionFrame->Label( -textvariable => \$directionText, -relief => 'flat', -borderwidth => 2, -padx=> 5, -pady => 5)->pack( -side => 'top'); $chooseDirectionFrame = $directionFrame->Frame( -relief => "flat", -borderwidth => 2)->pack( -side => "top", -fill => "y", -expand => 1); $chooseDirectionFrame->Radiobutton( -text => "Horizontal", -width => 10, -value => 0, -variable => \$sortDirection, -command => \&arrangeButtons)->pack( -side => "left", -padx => 5, -pady => 5); $chooseDirectionFrame->Radiobutton( -text => "Vertical", -width => 10, -value => 1, -variable => \$sortDirection, -command => \&arrangeButtons)->pack( -side => "left", -padx => 5, -pady => 5); $numItems = 13; $itemCountFrame = $mainWin->Frame( -relief => "ridge", -borderwidth => 2)->pack( -side => "top", -fill => "y", -expand => 1); $itemCountFrame->Label( -textvariable => \$itemsText, -relief => 'flat', -borderwidth => 2, -padx=> 5, -pady => 5)->pack( -side => 'top'); $chooseItemsFrame = $itemCountFrame->Frame( -relief => "flat", -borderwidth => 2)->pack( -side => "top", -fill => "y", -expand => 1); for (2 .. 27) { $chooseItemsFrame->Radiobutton( -text => "$_", -value => $_, -variable => \$numItems, -width => 3, -command => \&arrangeButtons)->grid( -row => (($_ - 2) / 5), -column => (($_ - 2) % 5), -padx => 5, -pady => 5); } $numRowsOrCols = 2; $rowColCountFrame = $mainWin->Frame( -relief => "ridge", -borderwidth => 2)->pack( -side => "top", -fill => "y", -expand => 1); $rowColCountFrame->Label( -textvariable => \$rowColCountText, -relief => 'flat', -borderwidth => 2, -padx=> 5, -pady => 5)->pack( -side => 'top'); $chooseColumnsFrame = $rowColCountFrame->Frame( -relief => "flat", -borderwidth => 2)->pack( -side => "top", -fill => "y", -expand => 1); for (2 .. 7) { $chooseColumnsFrame->Radiobutton( -text => "$_", -value => $_, -variable => \$numRowsOrCols, -command => \&arrangeButtons)->pack( -side => "left", -padx => 5, -pady => 5); } $rlButtons = []; $buttonFrame = $mainWin->Frame( -relief => "ridge", -borderwidth => 2)->pack( -side => "top", -fill => "y", -expand => 1); $controlButtonFrame = $mainWin->Frame( -relief => "flat", -borderwidth => 2)->pack( -side => "top"); $controlButtonFrame->Button( -text => "Quit", -background => "SlateBlue3", -activebackground => "turquoise1", -foreground => "yellow1", -command => sub {exit;})->pack( -side => "right", -padx => 5, -pady => 5); arrangeButtons(); MainLoop(); sub arrangeButtons { foreach my $button (@$rlButtons) { $button->destroy() if Exists($button); } setRowOrColumnText(); setDirectionText(); setItemsText(); setRowColCountText(); my $rlButtonOrder = $rowsOrColumns ? # 1 - cols., 0 - rows ($sortDirection ? # 1 - vert., 0 - hor. fitToColsVSort($numItems, $numRowsOrCols) : fitToColsHSort($numItems, $numRowsOrCols)) : ($sortDirection ? # 1 - vert., 0 - hor. fitToRowsVSort($numItems, $numRowsOrCols) : fitToRowsHSort($numItems, $numRowsOrCols)); foreach my $buttonNo (1 .. $numItems) { my $buttonName = "Button_" . $buttonNo; $$buttonName = $buttonFrame->Button( -text => "Button $buttonNo", -width => 9)->grid( -row => $rlButtonOrder->[$buttonNo - 1]->[0], -column => $rlButtonOrder->[$buttonNo - 1]->[1], -padx => 5, -pady => 5); push @$rlButtons, $$buttonName; } } sub setRowOrColumnText { $rowColText = "By " . (qw(Rows Columns))[$rowsOrColumns] . " ..."; } sub setDirectionText { $directionText = "... sorting " . (qw(horizontally vertically))[$sortDirection] . " ..."; } sub setItemsText { $itemsText = "... fit $numItems items ..."; } sub setRowColCountText { $rowColCountText = "... to $numRowsOrCols " . (qw(rows columns))[$rowsOrColumns]; }
There's not a lot in the way of comments, it was not production code, sorry.
Cheers,
JohnGG
|
|---|