使用TCL理解TK TreeCtrl中的项目创建
因此,我试图学习TK tree ctrl,并获得了大量在线文档和示例代码,但不幸的是,其中没有一个解释某些代码背后的逻辑。因此,我创建了一个tk树,其中包含一列、元素和一些项目,如下所示:使用TCL理解TK TreeCtrl中的项目创建,tree,tcl,tk,Tree,Tcl,Tk,因此,我试图学习TK tree ctrl,并获得了大量在线文档和示例代码,但不幸的是,其中没有一个解释某些代码背后的逻辑。因此,我创建了一个tk树,其中包含一列、元素和一些项目,如下所示: set Priv(dlg) [toplevel .topLevel]; set recess $Priv(dlg); set Priv(tree1) [treectrl $recess.tree1] grid $Priv(tree1) -sticky news; grid columnconfigure
set Priv(dlg) [toplevel .topLevel];
set recess $Priv(dlg);
set Priv(tree1) [treectrl $recess.tree1]
grid $Priv(tree1) -sticky news;
grid columnconfigure $recess 0 -weight 1
grid rowconfigure $recess 0 -weight 1
::at::BOMComparison::CreateElems $Priv(tree1)
for {set i 0} {$i < 100} {incr i} {
set parent [expr {int(rand()*$i)}] #THIS IS WHERE I GET CONFUSED
$Priv(tree1) item create -tag item$i -button auto
$Priv(tree1) item lastchild $parent item$i
$Priv(tree1) item text item$i name item$i
}
proc BOAComparison::CreateElems {T} {
$T element create rect rect -fill [list grey 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;
}
对此
set parent "Module"
我得到一个错误,说项目“模块”不存在。因此,我试图找出$parent是一个整数与它是一个字符串名之间的区别。我尝试了这个例子,但没有找到解释,我想知道是否有人可以帮我解释一下
好吧,根据你的解释,我试过这样的方法船长:
但是如果我删除了从id中提取名称的两个语句,即
set parent [::getName $parent];
set child [::getName $child];
然后,我得到项目的适当层次结构,如下所示:
- 十,
- 二十
- 二十一,
- 二十四
- 二十二
- 二十五
- 二十一,
- 二十
Id
创建项目-正是该Id
用于定义父项目。在树填充代码中,集合父项[expr{int(rand()*$i)}]
得到一个整数,该整数在先前创建的项目范围内(它们的编号从1开始-我们发现一个随机整数小于从0开始的$i
)
如果您创建了一个名为“Module”的项目,则需要找到其
Id
以用作parent
属性(您可以使用$Priv(tree1)item Id“Module”
-或保存创建该项目时的返回值)谢谢船长,我发现了我的错误。我应该将项目名称更改为项目名称,其余的应该是id的唯一名称。我试过了,现在可以用了。再次感谢。
set treeHierarchy [::getParentChildRel]; #This returns me a list of parent child relation. {{10 20} {20 21} {20 22} {21 24} {20 25}}. Here 10, 20... are ids of parent and child and these ids are associated with the string names of parent and child so i can get the parent child names out of these ids.
foreach item $modHierarchy {
lassign $item parent child;
set parent [::getName $parent]; #This returns the string name associated with the id. Eg. ModulaA is name associated with id 10.
set child [::getName $child]; #Same goes for this
if {[$T item id "tag $parent"] eq ""} { #Create parent if not existing and make it child of root
set p [$T item create -tag $parent -button auto];
$T item text $p name $parent
$T item lastchild root $p
}
$T item create -tag $child -button auto
$T item lastchild "$parent" "$child" # BUT I GET THE SAME ITEM "Module*" DOESN'T EXIST ERROR HERE. NOT SURE WHY.
$T item text "tag $child" name $child
}
set parent [::getName $parent];
set child [::getName $child];