【问题标题】:tcl/tk panedwindows, how to define a percentage size for child windows so that manual resizing keeps the display ratio?tcl/tk panedwindows,如何定义子窗口的百分比大小,以便手动调整大小保持显示比例?
【发布时间】:2019-04-14 00:17:21
【问题描述】:

我对 tk 界面构建相对较新,在开始编写我需要的东西之前尝试弄清楚一些东西

在下面找到一个愚蠢的布局作为示例

我想要实现的是,在调整根窗口或(任何子窗格窗口)的大小时,内部的所有子窗口都会调整大小,同时保持 50% 的空间比

在 atm 时,例如,当我将根窗口调整到左侧时,黄色文本框会缩小直到完全消失(蓝色文本框以此类推)

我想要的是我的两个半窗格(黄色的一个和包含黑色、蓝色和绿色的一个)在每一半上保持均匀显示

显然其他 2 个嵌套的 panedwindows 相同

似乎在某个时间点 panedwindows 曾经接受一个“分数”参数,但它似乎不再允许了

提前感谢您的帮助,如果我的代码太笨拙,而您想提供一个改进的代码以及对我的问题的回答,非常感谢

#!/bin/sh
#The next line executes wish - wherever it is \
exec wish "$0" "$@"

proc bindings {} {
    bind . <Alt-Key-x> exit
    bind . <Alt-Key-k> redraw
    bind . <Key-Escape> exit

    bind . <Key> { puts [focus] }
}       

proc redraw {} {
    # which window has focus
    set foc [focus]

    puts "kill and redraw focused windows $foc"

    destroy $foc

    if { [string equal $foc ".t1"] } {
        upleftbox
        .sp3 paneconfigure .t1 -before .t2
    } elseif { [string equal $foc ".t2"] } {
        uprightbox
        .sp3 paneconfigure .t2 -after .t1
    } elseif { [string equal $foc ".t3"] } {
        bottombox
        .sp2 paneconfigure .t3 -after .sp3
    } elseif { [string equal $foc ".t4"] } {
        sidebox
       .sp1 paneconfigure .t4 -after .sp2
    }       
}

proc scrolled_text { f args } {
    #----------------------------------------------
    # scrolled_text from Brent Welch's book
    #----------------------------------------------

    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 nsew
    grid $f.xscroll -sticky nsew
    grid rowconfigure $f 0 -weight 1
    grid columnconfigure $f 0 -weight 1
    return $f.text
}

# structure is .sp1.sp2.sp3 
# defining the layout
proc sp1 {} {
    panedwindow .sp1 -orient h -opaqueresize 0 -sashwidth 10
}

proc sp2 {} {
    panedwindow .sp2 -orient v -opaqueresize 0 -sashwidth 10
}

proc sp3 {} {
    panedwindow .sp3 -orient h -opaqueresize 0 -sashwidth 10
}

# demo textboxes to populate the 4 windows GUI
proc upleftbox {} {
    text .t1 -background black -foreground lightgreen -width 18 -height 8
}

proc uprightbox {} {
    text .t2 -background blue -foreground lightgreen -width 18 -height 8
}

proc bottombox {} {
    text .t3 -background green -foreground lightgreen -width 18 -height 8
}

proc sidebox {} {
    text .t4 -background yellow -foreground lightgreen -width 18 -height 8
}

# Main

# define basics stuff
tk_setPalette background black foreground lightgreen
wm title . paf.tk
wm minsize . 50 60

bindings

# building basic layout

# call the windows proc
sp1
sp2
sp3
sidebox
bottombox
uprightbox
upleftbox

# build the windows like russian dolls

grid .sp1 -in . -sticky nsew 

.sp1 add .sp2 .t4
.sp2 add .sp3 .t3
.sp3 add .t1 .t2

# set the weight for expanding when resizing on the fly
grid columnconfigure . .sp1     -weight 1
grid rowconfigure    . .sp1     -weight 1

【问题讨论】:

  • TCL\TK Resize window : bind 的可能重复项。您可以在这里使用相同的原理。您可以将新绑定添加到您的bindings proc 并调整您需要调整大小的面板。
  • 感谢您的回复 :) 我正在审查这两个 atm,在我接受之前看看哪一个最合适。我明白了,但是我想知道的是,如果用户任意调整不同文本框的大小,而不是从一开始就固定和确定,然后根调整大小,必须保持这些比率,这将是多么方便。在这种情况下,我需要在任何时间点获取每个子窗口的比率,以便我可以保持任意设置的比率(如果是这样,可行),除非我错过了什么
  • 我认为可以按照您的建议完成,但除非我寻找的东西太复杂而无法做到,或者我只是不够擅长编码,否则可能很难处理复杂的布局,所以我会保持简单并使用 ttk:panedwindows 代替。无论如何谢谢:)

标签: tcl tk percentage


【解决方案1】:

您可以使用ttk::panedwindow 代替panedwindow。它的add 子命令有-weight 选项,它会根据子窗口的权重自动调整子窗口的大小。

【讨论】:

  • 感谢您的回复 :) 我正在审查两个 atm,在我接受之前看看哪个最合适
  • ttk::panedwindow 是实现我想要的最简单的方法,谢谢队友:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-01
  • 2020-05-12
  • 1970-01-01
  • 2012-08-25
  • 1970-01-01
  • 1970-01-01
  • 2021-10-05
相关资源
最近更新 更多