Increase the height of a text widget to get a full view of its word wrapped contents
fr As I don't know how to get the linecount of a word wrapped text widget, this code increases the widget's height until the bounding box of the last character returns a non empty list. (MG In Tk 8.5, I think you can use $textWidget count -displaylines 1.0 end)
package require Tk
bind . <F1> {console show}
wm geometry . 260x600
proc geom {x} {
wm geometry . [format %dx600 $x]
}
button .br -text "reset text" -command reset
pack .br -pady 5
if {[expr {$tcl_version >= 8.5}]} { ;# as per MG's tip
button .bv -text "full view" -command {.t configure -height [.t count -displaylines 1.0 end]}
} else {
button .bv -text "full view" -command adapt
}
pack .bv -side top
set svar 255
scale .sc -orient vertical -label "window width" -command geom -from 80 -to 800 -variable svar
pack .sc -side left
proc reset {} {
catch {destroy .t}
text .t -width 10 -wrap word -height 1
pack .t -side left -fill x -expand 1 -pady 5 -padx 5
for {set x 1} {$x<5} {incr x} {
if {$x>1} {.t insert end \n}
set first *
for {set y 0} {$y < 21} {incr y} {
.t insert end [format "%s%d.%02d" $first $x $y]
set first " "
}
.t insert end \n
}
}
proc adapt {{maxloop 100}} {
.t see 1.0
update idletasks
set pos [.t index end]
foreach {line col} [split $pos .] break
incr line -1
while {![llength [.t bbox $line.end]]} {
incr maxloop -1
if {!$maxloop} {
wm title . $h:aborted
break
}
set h [.t cget -height]
incr h
.t configure -height $h
wm title . [list $h height of .t]
update idletasks
}
}
reset