Version 2 of Geometry Managers

Updated 2001-11-05 16:32:48

* What are geometry managers - when do you use one?

  • What geometry managers come with Tk? place - pack - grid
  • How do you decide to use one over another? RS: grid for tabular layouts, mostly pack; hardly ever place
  • What other geometry managers are available?

QUESTION - Harald Kirsch

Is it possible with place to position a widget .f.bot just below another widget .f.top such that the following holds:

  1. If the container .f is resized vertically, both widgets keep their natural height and keep sticked to the top of .f
  2. If .f is resized horizontally, .f.top keeps it natural width and stays sticked to the left border of .f while .f.bot resizes itself to stretch over the whole width of .f.

Please note that 2 seems to make it impossible to use the sibling-trick mentioned in the place-manual.

I would probably get the above done with place, if I knew a way of extracting the natural width and height of widgets which are [place]d. Neither [... cget -width] nor [winfo width ...] deliver anything useful - except if I call update before calling winfo.

ANSWER - DKF

You need to use [winfo reqwidth]. Code that satisfies your request follows:

  # Basic Setup
  frame .f -bg red
  label .f.top -text "I'm top" -bg yellow
  label .f.bot -text "I'm bot" -bg white
  pack .f -fill both -expand 1
  # Geometry Manager Magic
  set w1 [winfo reqwidth .f.top]
  set w2 [winfo reqwidth .f.bot]
  if {$w1<$w2} {set w1 $w2}
  set h [expr {[winfo reqheight .f.top]+[winfo reqheight .f.bot]}]
  .f configure -width $w1 -height $h
  place .f.top -x 0 -y 0
  place .f.bot -x 0 -y [winfo reqheight .f.top] -relwidth 1

Did you know that you can also achieve the same effect using [grid]?

  # Basic Setup
  frame .f -bg red
  label .f.top -text "I'm top" -bg yellow
  label .f.bot -text "I'm bot" -bg white
  pack .f -fill both -expand 1
  # Geometry Manager Magic
  grid .f.top -sticky w
  grid .f.bot -sticky ew
  grid columnconfigure .f 0 -weight 1
  grid rowconfigure .f 2 -weight 1 -minsize 0

And also [pack]?

  # Basic Setup
  frame .f -bg red
  label .f.top -text "I'm top" -bg yellow
  label .f.bot -text "I'm bot" -bg white
  pack .f -fill both -expand 1
  # Geometry Manager Magic
  pack .f.top -side top -anchor w
  pack .f.bot -side top -anchor nw -fill x -expand 1

Many widgets have -width and -height options, but those do not usually give useful results when read (for the purposes of geometry management.) The [winfo width] and [winfo height] commands would be more useful you'd think, but in fact they only ever report the current size of a widget, which is initially zero as there hasn't been time to work it out yet (since interaction with geometry managers is performed on idle updates.) To get the info you need for GM, you need the [winfo reqheight] and [winfo reqwidth] commands, which report what size widgets would prefer to be.

The other thing you need to watch out for if you are doing any fancy geometry mangling (unlike the simple example above) is the <Configure> event. This lets you find out when the enclosing widget changes in size due to whatever GM is managing it. A sophisticated example that uses the configure events is available at [L1 ].

DKF


KBK: You also need to include update idletasks in the case where another geometry manager is managing the reqwidth or reqheight of the widget. It is possible that the requested size is zero because a subordinate geometry manager hasn't had the chance to work out the size.

KBK [L2 ]