Version 5 of They aren't wide enough

Updated 2003-12-20 12:49:52

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 ...].


PWQ '20 Dec 03' Can you give a real example of when the size of a widget (such as a label) need to be specifed. Without one my comment is that this is completely unnecessary. Either you want the label to be shown in full or not. If not then the label text should be truncated before displaying. (ie Hippo...) I get annoyed when I see TCL apps that people have assummed the width based on the font they have and it truncates on my display. This is the sort of bullsh?t that you get with VB applications.

I appreciate the interest. Here are two model situations with which I struggle: * I want two buttons to have the same width on-screen, although their -text-s differ. How do I get a button to pad itself out so it's as wide as another button? I'd love to know a way. * I have a label whose -text varies occasionally. I need its width to be constant, so my layout doesn't jerk around; I also need for the width to be big enough to allow for all the -text-s that will occur. Do you have good -width-free ways to code these?


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.