将Tk网格小部件集成到笔记本布局中

问题描述 投票:0回答:1

我想快速构建一个GUI原型,并认为Tk可以很容易学习。但是,我无法将示例网格视图(框架)集成到笔记本小部件的其中一个选项卡中。 pack命令将网格置于笔记本顶部,我无法找出正确的选项。或者我的方法总是错的?

这是代码:

ttk::frame .c
ttk::frame .c.f -borderwidth 5 -relief sunken -width 200 -height 100
ttk::label .c.namelbl -text Name
ttk::entry .c.name
ttk::checkbutton .c.one -text One -variable one -onvalue 1; set one 1
ttk::checkbutton .c.two -text Two -variable two -onvalue 1; set two 0
ttk::checkbutton .c.three -text Three -variable three -onvalue 1; set three 1
ttk::button .c.ok -text Okay
ttk::button .c.cancel -text Cancel

grid .c -column 0 -row 0
grid .c.f -column 0 -row 0 -columnspan 3 -rowspan 2
grid .c.namelbl -column 3 -row 0 -columnspan 2
grid .c.name -column 3 -row 1 -columnspan 2
grid .c.one -column 0 -row 3
grid .c.two -column 1 -row 3
grid .c.three -column 2 -row 3
grid .c.ok -column 3 -row 3
grid .c.cancel -column 4 -row 3

# Notebook --> shall contain above grid in third tab
ttk::notebook .n  -width 600 -height 200
ttk::frame .n.f1; 
ttk::frame .n.f2; 
.n add .n.f1 -text "FirstTab"
.n add .n.f2 -text "SecondTab"
.n add .c -text "GridContent"
pack [label .n.f1.f1 -background red -foreground white -text "First"]
pack [label .n.f2.f2 -background red -foreground white -text "Second"]
pack .c 
pack .n 
ttk::notebook::enableTraversal .n
user-interface tcl prototype tk
1个回答
1
投票

ttk::notebook的内容小部件在堆叠顺序中必须高于笔记本才能正常工作,并且必须由笔记本本身管理,而不是由packgrid管理(尽管它们的内容可以按照您想要的任何方式进行管理);笔记本是一种特殊的几何管理器,也是一个小部件。 (Tk还有其他几个小部件也可以这样做。)

要修复堆叠顺序,请在.c小部件后创建.n小部件,或在创建raise .c后创建.n。请注意,父窗口小部件的子窗口(除了toplevels和menus在某些情况下)总是位于父窗口之上,并且始终由其父窗口绑定/剪切。

要解决管理问题,根本就不要pack .c;将它添加到笔记本电脑就足够了。您可以根据需要打包或网格化.c的内容。


通过这两个小修补程序,您的UI似乎可以正常工作。

ttk::frame .c
ttk::frame .c.f -borderwidth 5 -relief sunken -width 200 -height 100
ttk::label .c.namelbl -text Name
ttk::entry .c.name
ttk::checkbutton .c.one -text One -variable one -onvalue 1; set one 1
ttk::checkbutton .c.two -text Two -variable two -onvalue 1; set two 0
ttk::checkbutton .c.three -text Three -variable three -onvalue 1; set three 1
ttk::button .c.ok -text Okay
ttk::button .c.cancel -text Cancel

grid .c -column 0 -row 0
grid .c.f -column 0 -row 0 -columnspan 3 -rowspan 2
grid .c.namelbl -column 3 -row 0 -columnspan 2
grid .c.name -column 3 -row 1 -columnspan 2
grid .c.one -column 0 -row 3
grid .c.two -column 1 -row 3
grid .c.three -column 2 -row 3
grid .c.ok -column 3 -row 3
grid .c.cancel -column 4 -row 3

# Notebook --> shall contain above grid in third tab
ttk::notebook .n  -width 600 -height 200
ttk::frame .n.f1; 
ttk::frame .n.f2; 
.n add .n.f1 -text "FirstTab"
.n add .n.f2 -text "SecondTab"
.n add .c -text "GridContent"
raise .c;   # <<<< YES! YOU DO WANT THIS! <<<< YES! <<<< YES! <<<< YES! <<<<
pack [label .n.f1.f1 -background red -foreground white -text "First"]
pack [label .n.f2.f2 -background red -foreground white -text "Second"]
# pack .c;  # <<<< NO! YOU DO NOT WANT THIS! <<<< NO! <<<< NO! <<<< NO! <<<<
pack .n 
ttk::notebook::enableTraversal .n
© www.soinside.com 2019 - 2024. All rights reserved.