防止立即执行此代码?

Preventing instantaneous execution of this code?

我无法理解如何更新 Tk 中的小部件以反映正在进行的过程。 基本上,现在我的程序是递归地扫描一个目录,并且应该一次将它找到的每个 directory/file 输出到一个文本小部件。我的问题是,脚本没有这样做,而是找到所有目录,然后在完成后立即输出所有内容。这是代码:

proc scan {{dir .}} {
global num_items_found vs_list ec_list

foreach i [lsort [glob -nocomplain -dir $dir *]] {
    if {[file type $i]=="directory"} {
        .main.body.log insert end "Checking $i\n";
        scan $i;
    } else {
        if {[string tolower [file tail $i]] eq "buildlog.htm"} {
            lappend vs_list $i;
            incr num_items_found;
            .main.body.log insert end "Found $i\n";
        } elseif {[file extension $i] eq ".log"} {
            lappend ec_list $i;
            incr num_items_found;
            .main.body.log insert end "Found $i\n";
        }
    } 
}

return;
} 

我也在另一个过程中调用这个过程,我也在扫描之前禁用了一个按钮并在扫描之后重​​新启用它;但这也不起作用。该脚本不允许我的小部件在扫描过程执行之前完全更新?我怎样才能得到想要的结果?

谢谢!

Tk 中的大多数显示更新都发生在事件循环中。在您的情况下,直到完成目录遍历后才进入事件循环。您可能需要使用 update idletasks 命令。

来自手册:

The update idletasks command is useful in scripts where changes have been made to the application's state and you want those changes to appear on the display immediately, rather than waiting for the script to complete. Most display updates are performed as idle callbacks, so update idletasks will cause them to run. However, there are some kinds of updates that only happen in response to events, such as those triggered by window size changes; these updates will not occur in update idletasks.