我创建了两个树控件小部件和一个滚动条小部件。我有radiobuttons,让用户可以根据按钮选择选择要滚动的小部件。所以我创建了如下小部件:
package require Tk
package require treectrl
namespace eval ::at::GUI {
variable Priv;
variable OptionsRB;
set Priv(treePrimary) "";
set Priv(treeSecondary) "";
set Priv(treeScrollbar) "";
if { ![info exists OptionsRB] } {
set OptionsRB(optColor) "ColorOnDraw";
set OptionsRB(optHighlight) "HighlightBold";
set OptionsRB(matchColor) "NoColor";
set OptionsRB(optSelHighlight) "";
set OptionsRB(scrollTree) "LeftTree";
}
}
proc ::at::GUI::DrawGUI {} {
variable Priv;
set frm_treeFrame [ttk::labelframe .treeFrame -text "Tree Area"];
set Priv(treePrimary) [treectrl $frm_treeFrame.treePrimary]
set Priv(treeSecondary) [treectrl $frm_treeFrame.treeSecondary]
set Priv(treeScrollbar) [ttk::scrollbar $frm_treeFrame.sb_treeScroll -command "::at::GUI::yview"]
grid $Priv(treePrimary) $Priv(treeSecondary) $Priv(treeScrollbar) -sticky news;
grid columnconfigure $frm_treeFrame 0 -weight 1;
grid columnconfigure $frm_treeFrame 1 -weight 1;
grid rowconfigure $frm_treeFrame 0 -weight 1;
set frm_ST [ttk::labelframe .scrollTreeOptions -text "Scroll Option"];
set rb_leftTree [ttk::radiobutton $frm_ST.rb1 -text "Left Tree" -variable [namespace current]::OptionsRB(scrollTree) -value LeftTree -command [namespace current]::configScroll ]
set rb_rightTree [ttk::radiobutton $frm_ST.rb2 -text "Right Tree" -variable [namespace current]::OptionsRB(scrollTree) -value RightTree -command [namespace current]::configScroll ]
set rb_bothTree [ttk::radiobutton $frm_ST.rb3 -text "Both Tree" -variable [namespace current]::OptionsRB(scrollTree) -value BothTree -command [namespace current]::configScroll ]
grid $rb_leftTree -row 1 -padx {30 5} -pady 5 -sticky w
grid $rb_rightTree -row 2 -padx {30 5} -pady 5 -sticky w
grid $rb_bothTree -row 3 -padx {30 5} -pady 5 -sticky w
# Grid all the frames in main window.
grid $frm_treeFrame -sticky news;
grid $frm_ST -sticky news;
##############################
::at::GUI::CreateLemes $Priv(treePrimary)
::at::GUI::CreateLemes $Priv(treeSecondary)
foreach tree [list $Priv(treePrimary) $Priv(treeSecondary)] {
for {set i 0} {$i < 20} {incr i} {
set parent [expr {int(rand()*$i)}]
$tree item create -tag item$i -button auto
$tree item lastchild $parent item$i
$tree item text item$i name item$i
}
}
return;
}
proc ::at::GUI::CreateLemes {T} {
$T element create rect rect -fill [list blue selected]
$T element create name text
set S [$T style create nameStyle]
$T style elements $S {rect name};
$T style layout $S rect -detach yes -iexpand xy;
$T style layout $S name -detach no -iexpand xy -expand e;
$T column create -tag name -itemstyle $S -text Items
$T configure -treecolumn first;
}
proc ::at::GUI::configScroll {args} {
variable Priv;
variable OptionsRB;
if {$OptionsRB(scrollTree) eq "LeftTree"} {
$Priv(treePrimary) -yscrollcommand "::at::GUI::yset $Priv(treeScrollbar)";
} elseif {$OptionsRB(scrollTree) eq "RightTree"} {
$Priv(treeSecondary) -yscrollcommand "::at::GUI::yset $Priv(treeScrollbar)";
} elseif {$OptionsRB(scrollTree) eq "BothTree"} {
$Priv(treePrimary) -yscrollcommand "::at::GUI::yset $Priv(treeScrollbar)";
$Priv(treeSecondary) -yscrollcommand "::at::GUI::yset $Priv(treeScrollbar)";
}
}
proc ::at::GUI::yset {sb args} {
uplevel [linsert $args 0 $sb set]
::at::GUI::yview moveto [lindex [$sb get] 0]
}
proc ::at::GUI::yview {args} {
variable Priv;
variable OptionsRB;
parray OptionsRB;
if {![info exists $OptionsRB(scrollTree)]} {
puts "INFO DOESN'T EXIST.";
}
if {$OptionsRB(scrollTree) eq "LeftTree"} {
eval [linsert $args 0 $Priv(treePrimary) yview];
} elseif {$OptionsRB(scrollTree) eq "RightTree"} {
eval [linsert $args 0 $Priv(treeSecondary) yview];
} elseif {$OptionsRB(scrollTree) eq "BothTree"} {
eval [linsert $args 0 $Priv(treePrimary) yview];
eval [linsert $args 0 $Priv(treeSecondary) yview];
}
}
所以在::at::GUI::yview
过程中,我收到错误消息can't read "OptionsRB(scrollTree)": no such element in array while executing
"info exists $OptionsRB(scrollTree)" ...
。所以我尝试使用parray OptionsRB;
打印数组数据,结果是
OptionsRB(optColor) = ColorOnDraw
OptionsRB(optHighlight) = HighlightBold
OptionsRB(matchColor) = NoColor
OptionsRB(optSelHighlight) =
这很奇怪,因为它没有向我显示元素scrollTree。我不知道我是否遗漏了什么。我读到了数组密钥问题here,但这似乎是引用的问题所以不确定缺少什么。任何意见?
更新:即使我在我的命名空间set OptionsRB(scrollTree) "LeftTree";
中为此变量赋值,但默认情况下我的Left Tree
单选按钮未被选中,我确实看到了onething,我认为这是原因错误,但后来我不知道为什么没有选择默认的radiobutton。