【发布时间】:2022-09-26 02:53:17
【问题描述】:
在我努力创建一个简单的树视图 GUI 的过程中,我遇到了 tree.tcl,它在活动的 tcl 8.6 安装中可用。
我已经设法调整代码以适应我的目的(哈迪改变了任何东西),当我以与 Active TCL 8.6(通过小部件)的演示相同的方式运行代码时,代码按预期运行(尽管我\'没有尝试在树中选择任何节点)。
但是,一旦我将代码复制到我的 gui 应用程序中,情况就不是这样了。
结构符合预期,但是当我尝试扩展节点时,我得到:
-
我收到无效命令错误 错误:无效的命令名称 \"populateTree\" 绑定到事件的命令: \"populateTree .fc.tv.tree [.fc.tv.tree 焦点]\"
-
现在由于某种原因,文件夹中的所有文件都没有被读取,即所有文件类型都被识别为目录,因此节点下的所有内容都显示为“dummy”
-
我还想添加过滤器以仅读取特定文件类型,即 *.txt,如果这样做,则甚至不会读取文件夹。 即
foreach f [lsort -dictionary [glob -nocomplain -dir $path *]]到foreach f [lsort -dictionary [glob -nocomplain -dir $path *.txt]]如果有人可以提供帮助,我将不胜感激。
# temp dir to mimic Network dir set ::dir \"C:/Dev\" proc populateRoots {tree} { populateTree $tree [$tree insert {} end -text \"Network File\" \\ -values [list $::dir directory]] } ## Code to populate a node of the tree proc populateTree {tree node} { if {[$tree set $node type] ne \"directory\"} { return } set path [$tree set $node fullpath] $tree delete [$tree children $node] foreach f [lsort -dictionary [glob -nocomplain -dir $path *]] { set type [file type $f] set id [$tree insert $node end -text [file tail $f] \\ -values [list $f $type]] if {$type eq \"directory\"} { ## Make it so that this node is openable $tree insert $id 0 -text dummy ;# a dummy $tree item $id -text [file tail $f]/ } elseif {$type eq \"file\"} { set size [file size $f] set ttime [file mtime $f] ## Format the file size nicely if {$size >= 1024*1024*1024} { set size [format %.1f\\ GB [expr {$size/1024/1024/1024.}]] } elseif {$size >= 1024*1024} { set size [format %.1f\\ MB [expr {$size/1024/1024.}]] } elseif {$size >= 1024} { set size [format %.1f\\ kB [expr {$size/1024.}]] } else { append size \" bytes\" } $tree set $id size $size } } # Stop this code from rerunning on the current node $tree set $node type processedDirectory } # ## Create the tree and set it up ttk::treeview $tw.tree -columns {fullpath type size date time} -displaycolumns {size date time} \\ -yscroll \"$tw.vsb set\" -xscroll \"$tw.hsb set\" ttk::scrollbar $tw.vsb -orient vertical -command \"$tw.tree yview\" ttk::scrollbar $tw.hsb -orient horizontal -command \"$tw.tree xview\" $tw.tree heading \\#0 -text \"Directory Structure\" $tw.tree heading size -text \"File Size\" $tw.tree column size -stretch 0 -width 70 populateRoots $tw.tree bind $tw.tree <<TreeviewOpen>> {populateTree %W [%W focus]} # ## Arrange the tree and its scrollbars in the toplevel lower [ttk::frame $tw.dummy] pack $tw.dummy -fill both -expand 1 grid $tw.tree $tw.vsb -sticky nsew -in $tw.dummy grid $tw.hsb -sticky nsew -in $tw.dummy grid columnconfigure $tw.dummy 0 -weight 1 grid rowconfigure $tw.dummy 0 -weight 1提前致谢, 乔治