blt::table(3tcl)

blt::table(3tcl) β€” straight from the BLT Built-In Commands manual. Okay so bestie, this is the table geometry manager and it's literally about to make your widget layout look like it was styled by a main character. πŸŽ€βœ¨


✨ NAME ✨

table β€” arranges widgets in a table πŸ’…


🌸 SYNOPSIS 🌸

table container ?widget index option value?...

table arrange container

table cget container ?item? option

table configure container ?item?... ?option value?...

table extents container item

table forget widget ?widget?...

table info container item

table locate container x y

table containers ?switch? ?arg?

table save container

table search container ?switch arg?...

πŸ’– DESCRIPTION πŸ’–

The table command arranges widgets in a table, no cap. The alignment of widgets is determined by their row and column positions and the number of rows or columns that they span. It's giving organized. It's giving structured. It's giving geometry manager realness. πŸ¦‹


πŸ¦‹ INTRODUCTION πŸ¦‹

Okay so real talk: probably the most painstaking aspect of building a graphical application is getting the placement and size of the widgets just right. It usually takes <em>so many</em> iterations to align widgets and adjust their spacing. Why? Because managing the geometry of widgets is simply not a packing problem β€” it's also a graphical design problem. Attributes like alignment, symmetry, and balance are more important than minimizing the amount of space used for packing. Periodt. πŸ’…

The table geometry manager arranges widgets in a table. It's easy to align widgets (horizontally and vertically) or to create empty space to balance the arrangement of the widgets. Widgets β€” called slaves in the Tk parlance β€” are arranged inside a containing widget called the master. Widgets are positioned at row,column locations and may span any number of rows or columns. More than one widget can occupy a single location. Not me loving a flexible grid bestie 😭

The placement of widget windows determines both the size and arrangement of the table. The table queries the requested size of each widget. The requested size of a widget is the natural size of the widget β€” before the widget is shrunk or expanded. The height of each row and the width of each column is the largest widget spanning that row or column. The size of the table is in turn the sum of the row and column sizes. This is the table's normal size. Iconic behavior honestly. πŸ‘‘

The total number of rows and columns in a table is determined from the indices specified. The table grows dynamically as windows are added at larger indices. It literally expands to fit your dreams. 🌈


πŸŽ€ EXAMPLE πŸŽ€

The table geometry manager is created by invoking the table command.

# Create a table in the root window
table .

The window . is now the container of the table. Widgets are packed into the table and displayed within the confines of the container. She's contained but make it fashion. πŸ’–

You add widgets to the table by row and column location. Row and column indices start from zero. Zero, bestie. Not one. Not me making that mistake ever again πŸ™ˆ

label .title -text "This is a title"

# Add a label to the table
table . .title 0,0

The label .title is added to the table. We can add more widgets in the same way.

button .ok -text "Ok"
button .cancel -text "Cancel"

# Add two buttons
table . .ok 1,0
table . .cancel 1,1

Two buttons .ok and .cancel are now packed into the second row of the table. They each occupy one cell of the table. By default, widgets span only a single row and column. Lowkey polite of them. ✨

The first column contains two widgets, .title and .ok. By default, the widest of the two widgets will define the width of the column. However, we want .title to be centered horizontally along the top of the table. We can make .title span two columns using the configure operation.

# Make the label span both columns
table configure . .title -cspan 2

The label .title will now be centered along the top row of the table. Slay. πŸ’…

In the above example, we've created and arranged the layout for the table invoking the table command several times. Alternately, we could have used a single table command.

label .title -text "This is a title"
button .ok -text "Ok"
button .cancel -text "Cancel"

# Create and pack the table
table . \
    .title  0,0 -cspan 2 \
    .ok     1,0 \
    .cancel 1,1

The table will override the requested width and height of the container so that the window fits the table exactly. This also means that any change to the size of table will be propagated up through the Tk window hierarchy. That's main character energy for real. πŸ”₯ This feature can be turned off using the configure operation again.

table configure . -propagate no

You can also set the width or height of the table to a specific value. This supersedes the calculated table size. No cap, you can just decide.

# Make the container 4 inches wide, 3 inches high
table configure . -reqwidth 4i -reqheight 3i

If a widget is smaller than the cell(s) it occupies, the widget will float within the extra space. By default, the widget will be centered within the space, but you can anchor the widget to any side of cell using the -anchor configuration option. It's giving floaty. It's giving anchored. Choose your vibe. πŸ¦‹

table configure . .ok -anchor w

The -fill option expands the widget to fill the extra space either vertically or horizontally (or both). Ate.

# Make the title label fill the entire top row
table configure . .title -cspan 2 -fill x

# Each button will be as height of the 2nd row.
table configure . .ok .cancel -fill y

The width of .title will be the combined widths of both columns. Both .ok and .cancel will become as tall as the second row. They're growing, your honor. 😭

The -padx and -pady options control the amount of padding around the widget. Both options take a list of one or two values.

# Pad the title by two pixels above and below.
table configure . .title -pady 2

# Pad each button 2 pixels on the left, and 4 on the right.
table configure . .ok .cancel -padx { 2 4 }

If the list has only one value, then both exterior sides (top and bottom or left and right) of the widget are padded by that amount. If the list has two elements, the first specifies padding for the top or left side and the second for the bottom or right side. It's giving personal space, but make it precise. πŸ’–

Like the container, you can also override the requested widths and heights of widgets using the -reqwidth and -reqheight options. This is especially useful with character-based widgets (such as buttons, labels, text, listbox, etc) that let you specify their size only in units of characters and lines, instead of pixels. Not me finally getting my buttons to line up 😌

# Make all buttons one inch wide
table configure . .ok .cancel -reqwidth 1i

Each row and column of the table can be configured, again using the configure operation. Rows are and columns are designated by Ri and Ci respectively, where i is the index of the row or column. iykyk. πŸŽ€

For example, you can set the size of a row or column.

# Make the 1st column 2 inches wide
table configure . c0 -width 2.0i

# Make the 2nd row 1/2 inch high.
table configure . r1 -height 0.5i

The new size for the row or column overrides its calculated size. If no widgets span the row or column, its height or width is zero. So you can use the -width and -height options to create empty spaces in the table. Empty space is a design choice, bestie. 🌸

# Create an empty row and column
table configure . r2 c2 -width 1i

The -pady option lets you add padding to the top and bottom sides of rows. The -padx option adds padding to the left and right sides of columns. Both options take a list of one or two values.

# Pad above the title by two pixels
table configure . r0 -pady { 2 0 }

# Pad each column 4 pixels on the left, and 2 on the right.
table configure . c* -padx { 2 4 }

Notice that you can configure all the rows and columns using either R* or C*. Star power. ⭐

When the container is resized, the rows and columns of the table are also resized. Only the rows or columns that contain widgets (a widget spans the row or column) grow or shrink. The -resize option indicates whether the row or column can be shrunk or stretched. If the value is shrink, the row or column can only be resized smaller. If expand, it can only be resized larger. If none, the row or column is frozen at its requested size. Frozen. Like a vibe check that said no. πŸ’€

# Let the 1st column get smaller, but not bigger
table configure . c0 -resize shrink

# Let the 2nd column get bigger, not smaller
table configure . c1 -resize expand

# Don't resize the first row
table configure . r0 -resize none

The following example packs a canvas, two scrollbars, and a title. The rows and columns containing the scrollbars are frozen at their requested size, so that even if the frame is resized, the scrollbars will remain the same width. Obsessed with the commitment to consistency. πŸ’…

table . \
    .title   0,0 -cspan 3 \
    .canvas  1,1 -fill both \
    .vscroll 1,2 -fill y \
    .hscroll 2,1 -fill x
# Don't let the scrollbars resize
table configure . c2 r2 -resize none

# Create an empty space to balance the scrollbar
table configure . c0 -width .vscroll

Note that the value of the -width option is the name of a widget window. This indicates that the width of the column should be the same as the requested width of .vscroll. It's matching energies. 🫢

Finally, the forget operation removes widgets from the table.

# Remove the windows from the table
table forget .quit .frame

It's not necessary to specify the container. The table command determines the container from the widget name. She's independent like that. ✨

🫢 OPERATIONS 🫢

okay bestie, here's the entire roster of stuff you can do with a table. every single one, no skipping, we're doing this properly πŸ’…


  • table container ?widget index option value?... β€” adds the widget widget to the table at index. and index? it's a row,column position in the table, bestie. it has to be in the form row,column, where row and column are the actual row and column numbers, and they start counting from zero β€” so 0,0 is the upper-leftmost spot, the very first cell, the origin, the moment. ✨ also, row and column can be numeric expressions that get recursively evaluated, so you can be a little math gremlin about it. if there's no table yet for container, one gets created. widget is the path name of the window β€” which must already exist β€” that you want arranged inside container. and option and value? those are described in the WIDGET OPTIONS section, go look. πŸ‘‘
  • table arrange container β€” forces the table to compute its layout right now, immediately, no waiting. normally the table geometry manager is a chill little thing and waits until the next idle point before calculating the size of its rows and columns. this operation is for when you cannot wait, and it's useful for collecting the normal sizes of rows and columns, which are based on the requested widget sizes. πŸ”₯
  • table cget container ?item? option β€” returns the current value of the configuration option that's specific to item, given by option. item is either a row or column index, or the path name of a widget, and it can be in any form described in the configure operation below (iykyk, keep reading). if you don't pass an item argument at all, then the configuration option is for the table itself, main character energy. option may be any one of the options described in the appropriate section for that item. πŸ’–
  • table configure container item... ?option value?... β€” queries or modifies the configuration options specific to item. if no option is specified, this command returns a list describing all of the available options for item. if the argument item is omitted, then the configuration options you're touching are for the table itself. otherwise item must be either a row or column specification, or the path name of a widget. the following item types are available:
    • Ci β€” specifies the column of container to be configured. item must be in the form Cn, where i is the index of the column. see the COLUMN OPTIONS section. 🌸
    • Ri β€” specifies the row of container to be configured. item must be in the form Ri, where i is the index of the row. see the ROW OPTIONS section. 🌸
    • widget β€” specifies a widget of container to be queried. widget is the path name of a widget packed in container. see the WIDGET OPTIONS section.
    • No argument β€” specifies that the table itself is to be queried. see the TABLE OPTIONS section for a description of the option-value pairs for the table.

okay so the option and value pairs are specific to item. if option is specified with no value, then the command returns a list describing that one named option β€” and heads up, that list will be identical to the corresponding sublist of the value you'd get back if no option was specified, so no surprises. if one or more option-value pairs are specified, then the command modifies the given option(s) to have the given value(s), and in that case the command returns the empty string. slay. ✨

  • table extents container index β€” queries the location and dimensions of rows and columns in the table. index can be either a row or column index, or a table index. it returns a list of the x,y coordinates (that's the upperleft corner) and the dimensions (width and height) of the cell, row, or column. πŸ“
  • table forget widget ?widget?... β€” requests that widget no longer have its geometry managed. widget is the pathname of the window currently managed by some table. the window will be unmapped so that it no longer appears on the screen. if widget isn't currently managed by any table, an error message is returned; otherwise you get the empty string. bye bestie πŸ‘‹
  • table info container item β€” returns a list of the current configuration options for item. the list returned is exactly in the form that might be specified to the table command β€” which is iconic, because it means you can use it to save and reset table configurations. πŸ’Ύ item must be one of the following:
    • Ci β€” specifies the column of container to be queried. item must be in the form Cn, where n is the index of the column.
    • Ri β€” specifies the row of container to be queried. item must be in the form Ri, where i is the index of the row.
    • widget β€” specifies a widget of container to be queried. widget is the path name of a widget packed in container.
    • No argument β€” specifies that the table itself is to be queried.
  • table locate container x y β€” returns the table index (row,column) of the cell containing the given screen coordinates. the x and y arguments represent the x and y coordinates of the sample point being tested. basically: you give it a point, it tells you which cell lives there. πŸ”
  • table containers ?switch arg? β€” returns a list of all container windows matching a given criteria (using switch and arg). if you don't pass any switch and arg arguments, the names of all container windows are returned (only those using the table command, obviously). the following are valid switches:
    • -pattern pattern β€” returns a list of pathnames of all container windows matching pattern.
    • -slave window β€” returns the name of the container window of the table managing window. window must be the path name of a widget. if window is not managed by any table, the empty string is returned. πŸ™ˆ
  • table search container ?switch arg?... β€” returns the names of all the widgets in container matching the criteria given by switch and arg. container is the name of the container window associated with the table to be searched. the name of the widget is returned if any one switch-arg criteria matches. if no switch-arg arguments are given, the names of all widgets managed by container are returned. the following switches are available:
    • -pattern pattern β€” returns the names of any names of the widgets matching pattern.
    • -span index β€” returns the names of widgets that span index. a widget doesn't need to start at index to be included, it just has to span it. index must be in the form row,column, where row and column are valid row and column numbers.
    • -start index β€” returns the names of widgets that start at index. index must be in the form row,column, where row and column are valid row and column numbers.

πŸ’œ TABLE OPTIONS πŸ’œ

to configure the table itself, you omit the item argument when invoking the configure operation. like so:

table configure container ?option value?...

the following options are available for the table:

  • -padx pad β€” sets how much padding to add to the left and right exteriors of the table. pad can be a list of one or two numbers. if pad has two elements, the left side of the table is padded by the first value and the right side by the second value. if pad has just one value, both the left and right sides are padded evenly by the value. the default is 0. πŸŽ€
  • -pady pad β€” sets how much padding to add to the top and bottom exteriors of the table. pad can be a list of one or two numbers. if pad has two elements, the area above the table is padded by the first value and the area below by the second value. if pad is just one number, both the top and bottom areas are padded by the value. the default is 0. πŸŽ€
  • -propagate boolean β€” indicates if the table should override the requested width and height of the container window. if boolean is false, container will not be resized β€” container will be its requested size. the default is 1. πŸ‘‘

✨ WIDGET OPTIONS ✨

okay so bestie, widgets are configured by naming the widget when you invoke the configure operation. Like, the vibe is:

table configure container widget ?option value?...

widget has to be the path name of a window that’s already packed in the table associated with container. The following options are available for widgets: πŸ’…

  • -anchor anchor β€” anchors the widget to a particular edge of the cell(s) it resides in. This option only does anything if the space of the spans surrounding the widget is larger than the widget. anchor says how the widget will be positioned in that space. For example, if anchor is center, then the window is centered in the rows and columns it spans; if anchor is w, the window is aligned with the leftmost edge of the span. The default is center.
  • -columnspan number β€” sets the number of columns the widget will span. The default is 1.
  • -columncontrol control β€” specifies how the width of the widget should control the width of the columns it spans. control is either normal, none, or full. The default is normal.
    • none β€” the width of the widget is not considered.
    • full β€” only the width of the widget will be considered when computing the widths of the columns.
    • normal β€” indicates that the widest widget spanning the column will determine the width of the span.
  • -fill fill β€” specifies if the widget should be stretched to fill any free space in the span surrounding the widget. fill is either none, x, y, both. The default is none.
    • x β€” the widget can grow horizontally.
    • y β€” the widget can grow vertically.
    • both β€” the widget can grow both vertically and horizontally.
    • none β€” the widget does not grow along with the span.
  • -ipadx pixels β€” sets how much horizontal padding to add internally on the left and right sides of the widget. pixels must be a valid screen distance like 2 or 0.3i. The default is 0.
  • -ipady pixels β€” sets how much vertical padding to add internally on the top and bottom of the widget. pixels must be a valid screen distance like 2 or 0.3i. The default is 0.
  • -padx pad β€” sets how much padding to add to the left and right exteriors of the widget. pad can be a list of one or two numbers. If pad has two elements, the left side of the widget is padded by the first value and the right side by the second value. If pad has just one value, both the left and right sides are padded evenly by the value. The default is 0.
  • -pady pad β€” sets how much padding to add to the top and bottom exteriors of the widget. pad can be a list of one or two numbers. If pad has two elements, the area above the widget is padded by the first value and the area below by the second value. If pad is just one number, both the top and bottom areas are padded by the value. The default is 0.
  • -reqheight height β€” specifies the limits of the requested height for the widget. height is a list of bounding values. See the BOUNDING SIZES section for a description of this list. By default, the height of the widget is its requested height with its internal padding (see the -ipady option). The bounds specified by height either override the height completely, or bound the height between two sizes. The default is "".
  • -reqwidth width β€” specifies the limits of the requested width for the widget. width is a list of bounding values. See the BOUNDING SIZES section for a description of this list. By default, the width of the widget is its requested width with its internal padding (set the -ipadx option). The bounds specified by width either override the width completely, or bound the height between two sizes. The default is "".
  • -rowspan number β€” sets the number of rows the widget will span. The default is 1.
  • -rowcontrol control β€” specifies how the height of the widget should control the height of the rows it spans. control is either normal, none, or full. The default is normal.
    • none β€” the height of the widget is not considered.
    • full β€” only the height of the widget will be considered when computing the heights of the rows.
    • normal β€” indicates that the tallest widget spanning the row will determine the height of the span.

🌸 COLUMN OPTIONS 🌸

To configure a column in the table, specify the column index as Ci, where i is the index of the column to be configured. Like:

table configure container Ci ?option value?...

If the index is specified as C*, then all columns of the table will be configured. The following options are available for table columns:

  • -padx pad β€” sets the padding to the left and right of the column. pad can be a list of one or two numbers. If pad has two elements, the left side of the column is padded by the first value and the right side by the second value. If pad has just one value, both the left and right sides are padded evenly by the value. The default is 0.
  • -resize mode β€” indicates that the column can expand or shrink from its requested width when the table is resized. mode must be one of the following: none, expand, shrink, or both. If mode is expand, the width of the column is expanded if there is extra space in the container window. If mode is shrink, its width may be reduced beyond its requested width if there is not enough space in the container. The default is none.
  • -width width β€” specifies the limits within that the width of the column may expand or shrink. width is a list of bounding values. See the section BOUNDING SIZES for a description of this list. By default there are no constraints.

🌈 ROW OPTIONS 🌈

To configure a row in the table, specify the row index as Ri, where i is the index of the row to be configured. Like:

table configure container Ri ?option value?...

If the index is specified as R*, then all rows of the table will be configured. The following options are available for table rows:

  • -height height β€” specifies the limits of the height that the row may expand or shrink to. height is a list of bounding values. See the section BOUNDING SIZES for a description of this list. By default there are no constraints.
  • -pady pad β€” sets the padding above and below the row. pad can be a list of one or two numbers. If pad has two elements, the area above the row is padded by the first value and the area below by the second value. If pad is just one number, both the top and bottom areas are padded by the value. The default is 0.
  • -resize mode β€” indicates that the row can expand or shrink from its requested height when the table is resized. mode must be one of the following: none, expand, shrink, or both. If mode is expand, the height of the row is expanded if there is extra space in the container. If mode is shrink, its height may be reduced beyond its requested height if there is not enough space in the container. The default is none.

πŸ¦‹ BOUNDING SIZES πŸ¦‹

Sometimes it’s more useful to limit resizes to an acceptable range than to fix the size to a particular value or disallow resizing altogether. Similar to the way the wm command lets you specify a minsize and maxsize for a toplevel window, you can bound the sizes the container, a widget, row, or column may take. The -width, -height, -reqwidth, and -reqheight options take a list of one, two, or three values. We can take a previous example and, instead of preventing resizing, bound the size of the scrollbars between two values.

table . \
    .title   0,0 -cspan 3 \
    .canvas  1,1 -fill both \
    .vscroll 1,2 -fill y \
    .hscroll 2,1 -fill x

# Bound the scrollbars between 1/8 and 1/2 inch
table configure . c2 -width { 0.125 0.5 }
table configure . r2 -height { 0.125 0.5 }
table configure . vscroll .hscroll -fill both

The scrollbars will get no smaller than 1/8 of an inch, or bigger than 1/2 inch. The initial size will be their requested size, so long as it is within the specified bounds.

How the elements of the list are interpreted is dependent upon the number of elements in the list.

  • {} β€” empty list. No bounds are set. The default sizing is performed.
  • { x } β€” fixes the size to x. The window or partition cannot grow or shrink.
  • { min max } β€” sets up minimum and maximum limits for the size of the window or partition. The window or partition can be reduced less than min, nor can it be stretched beyond max.
  • { min max nom } β€” specifies minimum and maximum size limits, but also specifies a nominal size nom. This overrides the calculated size of the window or partition.

πŸŽ€ MISCELLANEOUS πŸŽ€

Another feature is that you can put two widgets in the same cell of the table. This is useful when you want to add decorations around a widget.

frame .frame -bd 1 -relief sunken
button .quit -text "Quit"

# Put both the frame and the button in the same cell.
table . \
    .quit  1,0 -padx 2 -pady 2 \
    .frame 1,0 -fill both

πŸ’€ LIMITATIONS πŸ’€

Okay bestie, gather round, because there's one iconic little gremlin in this whole situation and it's been haunting us since literally 1993. A long standing bug in Tk: there is no way to detect if a window is already a container of a different geometry manager. None. Zero. The technology does not exist. 🫠

And because Tk can't tell, this usually happens by accident, like the following β€” where all three widgets are arranged in the same container ., but using different geometry managers:

table .f1
...
pack .f2
...
grid .f3

Yeah. Three managers, one container. The audacity. 😭

This leads to bizarre window resizing, as each geometry manager applies its own brand of layout policies β€” and they are not coordinating, they are not talking, they are each doing their own thing like three group-chat members all typing at once.

And when the container is a top level window (such as .), your window manager may become locked as it responds to the never-ending stream of resize requests. Not cute. Not slay. Genuinely a hostage situation. πŸ™ˆ

So like... don't do that. One geometry manager per container. That's the vibe check. πŸ’…


🏷️ KEYWORDS 🏷️

frame, geometry manager, location, table, size


And that's the whole page, bestie!! πŸŽ€ You made it to the end, I'm so proud of you fr fr. This was pulled straight from BLT 2.5 and it's the blt::table(3tcl) man page β€” go forth and build the most immaculate layouts, just keep your geometry managers monogamous, mwah πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from