Search code examples
tcltk-toolkit

How to expand element into widget text in tcl/tk?


In this example, I want to expand the "frame" element so that it is the size of the text component, which receives it.

package require Tk
wm withdraw .

toplevel .top
pack [frame .top.frm] -side top -anchor center -ipadx 100 -ipady 100

set frame .top.frm.txt
pack [text $frame] -side top -anchor center

set panel [frame $frame.pnl]
pack [button $panel.btn -text button] -side top -anchor nw
pack [label $panel.lbl -text label] -side top -anchor nw

$frame window create end -window $panel

Since semantically or rather, the "-Expand True" property syntax is not accepted in this modality. Is there any solution for this?


Solution

  • The text widget (like the canvas widget) does not manage the size of the widgets that you embed within it (it does set the position of them). If you wish to make the size of the inner frame widget responsive to the size of the outer text widget, you need to add a <Configure> binding on the text widget and alter the size of the inner widget when that happens.

    For example:

    bind $frame <Configure> {
        %W.pnl configure -width [expr {%w - 5}] -height [expr {%h - 5}]
    }
    

    You also need to turn off (outward) geometry propagation for the $panel because you don't want the size of the panel being determined by its contents.

    pack propagate $panel 0