【问题标题】:Invalid Command and files not populating using treeview in TCL无效的命令和文件未在 TCL 中使用树视图填充
【发布时间】:2022-09-26 02:53:17
【问题描述】:

在我努力创建一个简单的树视图 GUI 的过程中,我遇到了 tree.tcl,它在活动的 tcl 8.6 安装中可用。

我已经设法调整代码以适应我的目的(哈迪改变了任何东西),当我以与 Active TCL 8.6(通过小部件)的演示相同的方式运行代码时,代码按预期运行(尽管我\'没有尝试在树中选择任何节点)。

但是,一旦我将代码复制到我的 gui 应用程序中,情况就不是这样了。

结构符合预期,但是当我尝试扩展节点时,我得到:

  1. 我收到无效命令错误 错误:无效的命令名称 \"populateTree\" 绑定到事件的命令: \"populateTree .fc.tv.tree [.fc.tv.tree 焦点]\"

  2. 现在由于某种原因,文件夹中的所有文件都没有被读取,即所有文件类型都被识别为目录,因此节点下的所有内容都显示为“dummy”

  3. 我还想添加过滤器以仅读取特定文件类型,即 *.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
    

    提前致谢, 乔治

    标签: treeview tcl


    【解决方案1】:

    问题是过程populateTree 必须是::populateTree,以便可以在命名空间中找到它。

    附言:我仍然无法将选择打印到控制台...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      • 2013-07-10
      • 2014-08-23
      • 1970-01-01
      相关资源
      最近更新 更多