Search code examples
texttclwidgettk-toolkit

Tcl/Tk: limiting the resize `text` widget


I have question on limiting the resizing the text Tk widget. I have the following code with two text widgets lined up on top of each other. The problem is when I resize the text widget containing "Box2" just disappears as shown in images below.

I want to do resizing such that "Box2" is also seen. If at certain stage of resizing if "Box2" can't be shown then resizing to a smaller size should be disallowed (though resizing to a bigger size should be allowed).

Normal size

This the normal sized one

Resized

Here "Box2" text widget disappears

The code to reproduce the problem is:

#----------------------------------------------
# scrolled_text from Brent Welch's book
#----------------------------------------------
proc scrolled_text { f args } {
    frame $f
    eval {text $f.text -wrap none \
        -xscrollcommand [list $f.xscroll set] \
        -yscrollcommand [list $f.yscroll set]} $args
    scrollbar $f.xscroll -orient horizontal \
        -command [list $f.text xview]
    scrollbar $f.yscroll -orient vertical \
        -command [list $f.text yview]
    grid $f.text $f.yscroll -sticky news
    grid $f.xscroll -sticky news
    grid rowconfigure $f 0 -weight 1
    grid columnconfigure $f 0 -weight 1
    return $f.text
}


proc horiz_scrolled_text { f args } {
    frame $f
    eval {text $f.text -wrap none \
        -xscrollcommand [list $f.xscroll set] } $args
    scrollbar $f.xscroll -orient horizontal -command [list $f.text xview]
    grid $f.text -sticky news
    grid $f.xscroll -sticky news
    grid rowconfigure $f 0 -weight 1
    grid columnconfigure $f 0 -weight 1 
    return $f.text
}
set st1 [scrolled_text .t1 -width 40 -height 10]
set st2 [horiz_scrolled_text .t2 -width 40 -height 2]

pack .t1 -side top -fill both -expand true
pack .t2 -side top -fill x 

$st1 insert end "Box1"
$st2 insert end "Box2"

Solution

  • Using grid instead of pack as suggested by schlenk works.

    set st1 [scrolled_text .t1 -width 80 -height 40]
    set st2 [horiz_scrolled_text .t2 -width 80 -height 2]
    
    grid .t1 -sticky news
    grid .t2 -sticky news
    
    # row 0 - t1; row 1 - t2
    grid rowconfigure . 0 -weight 10  -minsize 5
    grid rowconfigure . 1 -weight 2   -minsize 1
    grid columnconfigure . 0 -weight 1
    
    $st1 insert end "Box1"
    $st2 insert end "Box2"
    

    The key here is rowconfigure and the weight's assigned to it. I have assigned 10 to .t1 and 2 to .t2 in accordance to their height values. I have also set the minsize to 5 and 1 so that we do not shrink the window beyond a certain minimum.

    The columnconfigure has weight set to 1 because if we try to resize horizontally the windows should expand and fill instead of leaving empty spaces.