如何在程序运行时显示列表框或文本

发布于 2024-12-04 04:27:11 字数 625 浏览 0 评论 0原文

这是我的编码。我面临一个问题,即它仍在运行时没有显示“.display_message”。仅在 proc torun {} 完成其任务后我才收到消息。顺便说一句,2000年后实际上是我的程序在运行。这是一个很长的代码,我觉得它不适用,所以我删除了它以简化它。请在这方面指导我。谢谢。

proc torun {} {
    set total [.display_pc2 size]
    .display_message insert end "test" 


    for {set x 0} {$x < $total} {incr x} {
        set machine [.display_pc2 get $x]
            .display_message insert end "Copy to $machine now. Please wait..."
            .display_message see end
            after 2000
            .display_message insert end "Copy to $machine done"
            .display_message see end
            after 2000
    }
}

This is my coding. I am facing a problem where it didn't display the ".display_message" while it is still running. I receive the message only after the proc torun {} completed its task. Btw, after 2000 is actually my program running. It is a very long code where I feel it is not applicable so I removed it to simplified it. Please guide me in this. Thanks.

proc torun {} {
    set total [.display_pc2 size]
    .display_message insert end "test" 


    for {set x 0} {$x < $total} {incr x} {
        set machine [.display_pc2 get $x]
            .display_message insert end "Copy to $machine now. Please wait..."
            .display_message see end
            after 2000
            .display_message insert end "Copy to $machine done"
            .display_message see end
            after 2000
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇女薄笑 2024-12-11 04:27:11

您应该完全改变编写此代码的方式并使用空闲回调

这个想法是,您创建一个要执行的“任务”列表(可能是目标计算机的列表),将其保存在某处,然后一次处理一项,使用空闲回调重新安排执行。

草图如下:

proc show_transfer_start {target} {
  .display_progress add ... $target
}

proc show_transfer_result {res} {
  .display_progress add ... $res
}

proc schedule_transfer {target rest} {
  after idle [after 0 [list do_step $target $rest]]
}

proc do_step {target rest} {
  set res [copy --to $target]
  show_transfer_result $res
  if {[llength $rest] > 0} {
    set next [lindex $rest 0]
    show_transfer_start $next
    schedule_tranfer $next [lrange $rest 1 end]
  }
}

set targets [list box1 box2 box3]
set first [lindex $targets 0]
show_transfer_start $first
schedule_transfer $first [lrange $targets 1 end]

You should completely change the way you write this code and use idle callbacks.

The idea is that you create a list of "tasks" to do (probably that would be a list of targets computers), save it somewhere and then process it one item at a time, rescheduling the execution using idle callback.

A sketch follows:

proc show_transfer_start {target} {
  .display_progress add ... $target
}

proc show_transfer_result {res} {
  .display_progress add ... $res
}

proc schedule_transfer {target rest} {
  after idle [after 0 [list do_step $target $rest]]
}

proc do_step {target rest} {
  set res [copy --to $target]
  show_transfer_result $res
  if {[llength $rest] > 0} {
    set next [lindex $rest 0]
    show_transfer_start $next
    schedule_tranfer $next [lrange $rest 1 end]
  }
}

set targets [list box1 box2 box3]
set first [lindex $targets 0]
show_transfer_start $first
schedule_transfer $first [lrange $targets 1 end]
樱花落人离去 2024-12-11 04:27:11

我从来没有做过 tk (如果你正在做的是 gui),但当使用 puts 强制显示消息时,也许你需要一些与 flush 等效的东西。

因为我无法猜测 .display_message 过程中做了什么。

编辑:

刚刚有了一个想法:您可以使用after命令来伪造多线程您的应用程序。

after 0 [list .display_message insert end "立即复制到 $machine。请稍候..."; .display_message see end]

它将作为事件处理程序独立于当前进程运行。也许这可以解决您的冲水问题。 (需要 eventloop 或 update 命令)

I've never done tk (if it's gui what you're doing), but maybe you need some equivalent to flush when using puts to force-display a message.

Since I can't guess what's done inside the .display_message proc.

Edit:

Just got an idea: You can use the after command to fake-multithread your app.

after 0 [list .display_message insert end "Copy to $machine now. Please wait..."; .display_message see end]

Which will run independently from your current proc as event handler. Maybe that fixes your flush problem. (Requires the eventloop or update command)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文