Tcl/Tk:限制调整“文本”小部件的大小
我对限制 text
Tk
小部件的大小调整有疑问。我有以下代码,其中两个 text
小部件彼此排列在一起。问题是当我调整包含“Box2”的文本小部件的大小时,它就会消失,如下图所示。
我想调整大小,以便也可以看到“Box2”。如果在调整大小的某个阶段无法显示“Box2”,则应禁止调整为较小的尺寸(尽管应允许调整为较大的尺寸)。
正常尺寸
调整大小
重现问题的代码是:
#----------------------------------------------
# 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"
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
Resized
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
按照 schlenk 的建议,使用
grid
而不是pack
有效。这里的关键是 rowconfigure 并为其分配权重。我已根据
高度
将10
分配给.t1
,将2
分配给.t2
代码>值。我还将minsize
设置为5
和1
,以便我们不会将窗口缩小到超过某个最小值。columnconfigure
将weight
设置为1
,因为如果我们尝试水平调整大小,窗口应该扩展并填充,而不是留下空白空间。Using
grid
instead ofpack
as suggested by schlenk works.The key here is
rowconfigure
and the weight's assigned to it. I have assigned10
to.t1
and2
to.t2
in accordance to theirheight
values. I have also set theminsize
to5
and1
so that we do not shrink the window beyond a certain minimum.The
columnconfigure
hasweight
set to1
because if we try to resize horizontally the windows should expand and fill instead of leaving empty spaces.