Updated 2005-10-19 20:40:46

ulis, 2003-09-26: If you need to resize a text widget to show all the lines.

The script
  # create text
  text .t -width 20 -height 20 -wrap none
  pack .t -fill both -expand 1
  # randomly fill text
  set count [expr {int(rand() * 50)}]
  for {set i 0} {$i < $count} {incr i} \
  {
    set line [string repeat 0 [expr {int(rand()*50)}]]
    .t insert {end - 1 char} $line\n
  }
  # move the display to nw
  .t xview moveto 0.0
  .t yview moveto 0.0
  update
  # get visible area size in fractions of 1.0
  set fx [lindex [.t xview] 1]
  set fy [lindex [.t yview] 1]
  # get visible area size in chars
  set cw [.t cget -width]
  set ch [.t cget -height]
  # compute full size
  set width [expr {round($cw / $fx) + 2}]
  set height [expr {round($ch / $fy)}]
  # resize the widget
  .t config -width $width -height $height

Category GUI | Category Example