Tcl / Tk:限制调整大小`text`小部件

时间:2012-01-25 18:51:56

标签: text tcl widget tk

我对限制调整text Tk窗口小部件的大小有疑问。我有以下代码,两个text小部件排列在一起。问题是,当我调整包含“Box2”的文本小部件时,只会消失,如下图所示。

我想调整大小,以便也可以看到“Box2”。如果在调整大小的某个阶段,如果无法显示“Box2”,则应禁止调整大小(尽管应允许调整大小)。

正常尺寸This the normal sized one

已调整大小Here "Box2" text widget disappears

重现问题的代码是:

#----------------------------------------------
# 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"

1 个答案:

答案 0 :(得分:1)

按照schlenk的建议,使用grid代替pack

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"

此处的关键是rowconfigure,并为其分配了权重。我已根据10值将.t1分配给2,将.t2分配给height。我还将minsize设置为51,以便我们不会将窗口缩小到某个最小值。

columnconfigureweight设置为1,因为如果我们尝试水平调整大小,则窗口应该展开并填充,而不是留空空格。