tcl/tk treeview - 获取被点击节点的id

tcl/tk treeview - get the id of the node being clicked

在这个示例程序中,我可以获得各种信息,但是有没有一种简单的方法可以知道被点击的行的id

#! /usr/bin/env wish

ttk::treeview .tree -selectmode none

.tree tag bind clickable <ButtonRelease> {
  puts "%W %X %Y %K %b %d %A %K"
}

.tree configure -height 2

.tree insert {} end -id A -text A -tags clickable
.tree insert {} end -id B -text B -tags clickable

pack .tree

输出:

$ ./row-click.tcl 
.tree 894 407 ?? 1 ?? ?? ??
.tree 894 407 ?? 1 ?? ?? ??
.tree 893 431 ?? 1 ?? ?? ??

命令 .tree identify item $x $y 其中 $x$y 是坐标。在绑定脚本中,树名称最好指定为 %W,坐标为 %x%y(相对于小部件本身的坐标):

#! /usr/bin/env wish

ttk::treeview .tree -selectmode none

.tree tag bind clickable <ButtonRelease> {
  puts [%W identify item %x %y]
}

.tree configure -height 2

.tree insert {} end -id A -text A -tags clickable
.tree insert {} end -id B -text B -tags clickable

pack .tree