Tcl/Tk: How to scale the widgets when the user changes the size of the window

北慕城南 提交于 2019-12-12 00:32:51

问题


I have a window with widgets inside it. When I re-size the window manually, the widgets will scale them self when the height is reduced, but not when the width is changed or when the height is enlarged beyond the initial height. How do I make the widgets to be stack to the borders of the window?

The code for the creation of the window:

wm title $base "KitKite Sparam Viewer"
set frm_main      [frame  $base.main_frm]
pack $frm_main

grid [frame $frm_main.graph ] -row 1 -column 1
set g [sparam_graph_widget $frm_main.graph graph]

grid [set frm [frame $frm_main.frm]] -row 1 -column 2
#from and to frame
set from      [frame $frm.from -relief ridge -bd 2]
set from_lbl  [label $from.lbl -text "From:"]
set f_tbl_frm [frame $from.tbl_f]
set to        [frame $frm.to -relief ridge   -bd 2]
set to_lbl    [label $to.lbl   -text "To:"]
set t_tbl_frm [frame $to.tbl_t]

grid $from      -column 1 -row 1 -sticky nwe
grid $from_lbl  -row 1 -sticky nsew
grid $f_tbl_frm -row 2 -sticky nsew
grid $to        -column 2 -row 1 -sticky nwe
grid $to_lbl    -row 1 -sticky nsew
grid $t_tbl_frm -row 2 -sticky nsew

set from_t [sparam_table_widget $f_tbl_frm f_tbl]
set to_t   [sparam_table_widget $t_tbl_frm t_tbl]

set data        [frame $frm.data]
set data_lbl    [label $data.lbl -text "Choose data type to show"]
set isi         [checkbutton $data.cb_isi -variable cb(isi) -command [list __sp_data_changed isi $g] -text ISI       ]
set xt          [checkbutton $data.cb_xt  -variable cb(xt)  -command [list __sp_data_changed xt  $g] -text XT        ]
set ref         [checkbutton $data.cb_ref -variable cb(ref) -command [list __sp_data_changed ref $g] -text Reflection]
set conf_button [button $data.bt_conf -text "Configure connections" -command [list __sp_configure_datapath]]

grid $data        -column 1 -row 2 -columnspan 2 -sticky new
grid $data_lbl    -column 1 -row 1 -columnspan 2 -sticky nsew
grid $isi         -column 1 -row 2               -sticky nsw
grid $xt          -column 1 -row 3               -sticky nsw
grid $ref         -column 1 -row 4               -sticky nsw
grid $conf_button -column 2 -row 2 -rowspan 3    -sticky nsew

grid rowconfigure $frm 1 -weight 4 -uniform 1
grid rowconfigure $frm 2 -weight 1 -uniform 1
grid rowconfigure $frm_main 1 -weight 5 -uniform 1

$base is the name of the window created using toplevel sparam_graph_widget and sparam_table_widget are procedures that create and place inside the frame given to them custom widgets of a graph and a table (accordingly)


回答1:


In order for at least one widget to expand to consume the space, you need to set at least one column to have a non-zero weight.

grid columnconfigure $containerWidget $someWidgetOrIndex -weight 1

Note that once you do that, that column preferentially deals with all the size changes in the horizontal direction. You can adjust this by setting multiple columns to have non-zero weights, and give different columns different weights (-weight 2 will receive twice as large changes as -weight 1).

You might also need to adjust the options used to pack the outer frame. Debug by setting different frames to different colors so you can see what's going on…



来源:https://stackoverflow.com/questions/11757038/tcl-tk-how-to-scale-the-widgets-when-the-user-changes-the-size-of-the-window

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!