hide/show 小部件的最佳方法

best method for hide/show a widget

我想知道这种方法是否适合隐藏或显示小部件 也许是更好的方法?

提前致谢

ttk::treeview .tree

.tree insert {} end -id "Item 1" -text "Item 1"
.tree insert {} end -id "Item 2" -text "Item 2"
.tree insert {} end -id "Item 3" -text "Item 3"

text .text -bg green

button .b -text "H" -width 10 -command {grid .text -row 0}
button .b1 -text "B" -width 10 -command {grid .text -row 1}

wm geometry . 500x600

grid .tree -row 0 -columnspan 2 -sticky nsew
grid .text -row 1 -columnspan 2 -sticky nsew
grid .b -row 2 -sticky nsw
grid .b1 -row 2 -column 1 -sticky nse

grid columnconfigure . 0 -weight 1
grid rowconfigure . 0 -weight 1

您可以使用 grid remove。将按钮更改为:

button .b -text "H" -width 10 -command {grid remove .tree}
button .b1 -text "B" -width 10 -command {grid .tree}

当然如果你还想让文本区域扩展到第0行,你 可能还想修改文本区域的行。

grid remove 会记住配置设置。

参考文献:http://tcl.tk/man/tcl8.6/TkCmd/grid.htm

我认为这取决于你真正想要什么。如果您想保留原始 grid 配置以便再次使用此小部件,您应该使用 grid remove。但是如果你不打算在隐藏后使用小部件,你应该使用 grid forget