Version 3 of They aren't wide enough

Updated 2003-12-19 14:16:29

Sometimes I'm responsible for maintenance of an application that includes such elements as

    pack [button .b1 -text Lion -width 10] [button .b2 -text Hippopotamus -width 10]

[show picture here] Notice that there's a problem with this fragment; 'Hippopotamus' is truncated. That's not a good thing. Most immediately, it's because .b2 has been assigned a width that's too small. Generally in Tk it's OK to allow things to have default sizes; there are a couple of legitimate reasons to assign manifest sizes, though, including [... and ...--but people also mistakenly set sizes when ...].

If those widths are constant values, they need to be correct ones. Too-small labels make bad impressions on users. Here's a way I ask an application to tell me about itself: I source in

    proc walker {w procedure} {
      $procedure $w
      foreach child [winfo children $w] {
        walker $child $procedure
      }
    }


    proc check_width w {
      switch [winfo class $w] {
        Button -
        Label {
          set original_width [$w cget -width]
          set x_extent [winfo reqwidth $w]
          $w configure -width 0
          set needed_x_extent [winfo reqwidth $w]
          set result [expr $needed_x_extent > $x_extent]
          $w configure -width $original_width
          return $result
        }
        default {
          return 0
        }
      }
    }

then execute

    proc c w {
      if [check_width $w] {
        puts "Widget '$w' isn't wide enough."
      }
    }

    walker . c

The truth is, that's not exactly what I do, but it's close enough. For sufficiently weathered applications, and especially ones that have moved around between platforms, I usually pick up a few widgets that I'd rather find this way than by having a customer complain.