Tcl/Tk:限制调整“文本”小部件的大小

发布于 2024-12-28 17:45:56 字数 1678 浏览 0 评论 0原文

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

我想调整大小,以便也可以看到“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

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧竹 2025-01-04 17:45:56

按照 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代码>值。我还将 minsize 设置为 51,以便我们不会将窗口缩小到超过某个最小值。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文