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

Knowledgeable monks,
I am using 'grid' to display various widgets within a Tk toplevel widget. 'grid' lets me conceive of my widgets as buttons laid out on a spreadsheet, and indeed I use a spreadsheet to design the layout in the first place.

My problem: When I use the coordinates from my rows and columns as they appear on the spreadsheet, Tk seems to ignore all the "padding" I am trying to build in for my widget. It seems to collapse the widget's size, showing only the amount necessary to display the text label of the widget. Ideally, layout would appear exactly as on my spreadsheet, with all my row and column sizings using uniform units of space. For example, I display a button via the following:

$Output_b->grid( -column => 5, -row => 10, -columnspan => 4, -rowspan => 6, -sticky => 'nsew', );

but the output doesn't look at all like a 4x6 button. I'm considering putting empty widgets on rows 10 - 15 to make grid willing to span the six rows, but that doesn't seem like the proper way.

After this 'grid' section I have:

my ($cols, $rows) = $tl->gridSize(); for (my $i = 0; $i < $cols; $i++) { $tl->gridColumnconfigure($i, -weight => 1); } for (my $i = 0; $i < $rows; $i++) { $tl->gridRowconfigure($i, -weight => 1); }

which I thought would solve my problem but I can't tell as this does much. One of the button's text is not clipped and the buttons seem a little more uniform in the height of their rows, but....

Replies are listed 'Best First'.
Re: Want Tk to fully display 4x6 widget w/ grid instead of collapsing it
by Rich36 (Chaplain) on Oct 24, 2002 at 21:45 UTC

    Have you tried explicitly setting the -width and -height properties of the widgets? If the widgets are of a consistent size, then that should do the trick.


    «Rich36»
      Unfortunately, (or fortunately :-) the widgets are not of consistent size and so -width does not look like it will be useful within -grid. (One other twist is that I have 'styles' of layout, such that the button's appearance varies from style to style.)

      I do see that in configuring button $Output_b (and to handle multiple styles) I can do:

      -height => $style eq 'workstation' ? 6 : 3, # style eq something else...

      but now every other button on the same row as $Output_b has a minimum height of 3 text-rows (oops) and instead of fooling around with $Output_b's options I want to target all my adjustments within the -grid sections. I suppose I could throw in extra button configures as necessary within my -grid/style statement blocks, but ideally I shouldn't be distracted with altering other buttons as I configure the $Output_b button.

      Instead I have gone ahead with configuring dummy cells (blank Label widgits) within the 'style' that needs it, i.e.:

      my $cell_of_spaces = ' ' x 12; ## I've already set up the $Option_b button and am in the ## midst of configuring a "workstation" style/layout $Output_b->grid( -column => 5, -row => 9, -columnspan => 4, -rowspan => 6, -sticky => 'nsew', ); my $space_cell_col = 9; # column 9 already acted as a separator between two big # buttons, so no problem putting blank Labels here. foreach my $space_cell_row (9 .. 14) { build_blank_cell( $tl, $cell_of_spaces, $space_cell_col, $space_cell_row, ); } # Now that there are cells on each row between 9 and 14, # the $Output_b button's rowspan option takes full effect. sub build_blank_cell { my ( $tl, $cell_of_spaces, $space_cell_col, $space_cell_row ) = @_; my $new_label_widget_l = $tl->Label( -textvariable => \$cell_of_spaces, )->grid( -column => $space_cell_col, -row => $space_cell_row, -sticky => 'nsew', ); }
Want Tk to fully display 4x6 widget w/ grid instead of collapsing it
by rbc (Curate) on Oct 24, 2002 at 20:55 UTC
        Hmm; no, but a quick look at "perldoc Tk::Table" seems to indicate that I cannot make a widget "span" cells. Yes, I want fixed-size cells as building blocks, but in my 4x6 example my widget needs to cover the real estate of 24 such cells.
    Re: Want Tk to fully display 4x6 widget w/ grid instead of collapsing it
    by physgreg (Scribe) on Oct 25, 2002 at 13:04 UTC
      You need to change your gridColumnconfigure and gridRowconfigures to use the '-minsize' attribute. '-weight' just controls how the cells respond to the grid being resized. As an example:
      for (my $i = 0; $i < $rows; $i++) { $tl->gridRowconfigure($i, -weight => 1, -minsize => 80); }
      would make your cells always at least 80 pixels wide. All you have to do is make sure that minsize is greather than or equal to the largest widget.

      I have used this (many times!) in TCL/Tk, but not in Perl/Tk. However, I can't see why it wouldn't work.

        Oh beautiful for spacious columns!

        Thank you! I commented away the dummy cell creation and added a "-minsize => 13" to "gridRowconfigure". At first I played with "gridColumnconfigure" because I thought I had a "column" problem. But my "column" problem was actually stemming from the individual rows not having to maintain a minimum height. Code is now fixed per your suggestion. Thanks! :-)

        P.S. I start the toplevel with a certain geometry, which I must adjust a bit (as you forsaw). Since the original geometry didn't leave enough space for each row to accomodate the new -minsize, some rows had widgets whose text/height was cropped. Enlarging geometry from 630x423+113+74 made all my widgets happy again.

        for (my $i = 0; $i < $rows; $i++) { $tl->gridRowconfigure($i, -weight => 1, -minsize => 13 ); } $tl->geometry( 630x461+113+74 );