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

Integrate Tk grid widget into notebook layout

我想快速制作一个 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

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

要修复堆叠顺序,请在 .n 小部件之后创建 .c 小部件,或者在创建 .n 之后创建 raise .c。请注意,父控件的子控件(toplevel 和某些情况下的 menu 除外)始终位于父控件之上,并且始终由其父控件 bounded/clipped。

要解决管理问题,不要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