Main Content

Expanding a grid - Tcl/Tk

Archive - Originally posted on "The Horse's Mouth" - 2007-09-07 05:42:42 - Graham Ellis

Have you ever tried to fill or expand a grid in Tcl/Tk to fill the available space left by wider or taller widgets to the die, or when the window expands, and got results like those I have posted here? Disappointing results, aren't they? And I appear to have done all that I should in the source too:


label .title -text "This is a label" -relief ridge
frame .boxes
label .boxes.tl -text A -relief sunken
label .boxes.tr -text B -relief raised
label .boxes.bl -text C -relief flat
button .boxes.br -text D -relief groove -command exit
 
pack .title .boxes -fill both -expand yes
grid .boxes.tl .boxes.tr -sticky news
grid .boxes.bl .boxes.br -sticky news


The problem lies in the fact that Tk doesn't know HOW to expand the grid.

This is the improved - filled - version; I have used rowconfigure and columnconfigure on the grid command to specify how the extra space is to be distributes - evenly on the verticals and unevenly on the horizontals, as a demonstration.


Here are the extra 3 lines of code:

grid rowconfigure .boxes {0 1} -weight 1
grid columnconfigure .boxes 0 -weight 3
grid columnconfigure .boxes 1 -weight 1


This example is also useful to illustrate some of the different decoration types - "reliefs" - that can be applied to widgets. In a production piece of code, you would use only one or two of them - this training example is illustrative but will make the "look and feel" team shudder!