2012-01-25 48 views
1

我對限制調整大小textTk小部件有疑問。我有兩個排列在一起的小部件,其中包含兩個text代碼。問題是,當我調整包含「Box2」的文本窗口小部件時,會消失,如下圖所示。Tcl/Tk:限制調整「文本」部件的大小

我想調整大小,使「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" 
+2

嘗試使用'grid'而不是使用.t1/.t2包,並且在行/列上設置weight /和/ minsize選項。有關網格的更多信息,請參見http://www.tkdocs.com/tutorial/grid.html。 – schlenk

+0

@schlenk感謝您的提示。有用。我將發佈工作代碼作爲答案。 – Anand

回答

1

使用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和重量的分配給它。根據它們的height值,我已將10分配到.t12.t2。我還將minsize設置爲51,這樣我們就不會將窗口縮小到一定的最小值以下。

columnconfigure已將weight設置爲1因爲如果我們嘗試水平調整大小,窗口應該展開並填充而不是留下空白空間。

相關問題