繁体   English   中英

如何在 tcl / tk 中开发对话框仪表,通过移动它来获取循环值并在缩放小部件中更新

[英]How to develop dialog gauge in tcl / tk that gets loop value and updates in scale widget by moving it

我正在尝试制作一个进度条对话框,例如 GNU/Linux Xdialog --gauge

显然,您应该得到一个不时更新“一秒钟”的值。

这个增量是通过一个已经预先配置的for循环来完成的,以给出渐进计数。

从我所做的测试来看,我对tcl/tk的了解有多远 go 是这样的:

proc bar { pos } {
global txt
set txt $pos
}

set btn [button .btn -text "Play" -relief groove -command { bar $pos }]

scale .scl -length 200 -width 5 -orient horizontal -from 0 -to 100 -resolution 1 -variable value -sliderlength 5 -sliderrelief flat -activebackground blue -command { bar } -showvalue 0

label .lbl -textvariable txt -width 5

grid $btn -row 0 -column 0
grid .scl -row 0 -column 1
grid .lbl -row 0 -column 2

global value
for {set value 0} {$value < 10} {incr value} {  
after 1000; # delay
if {$value < 10} { puts "Number: $value" }
bind . "$btn invoke"
}

这有效,所以在控制台中..它没有显示表格 window ,比例小部件正在缓慢移动。 所以我需要最有经验的人的帮助我怎么能得到这个?

我创建了一个多帧 animation 以获得更好的想法。 看:

在此处输入图像描述

问题是你有一个同步延迟循环。 您需要一个异步的,以便可以处理显示更新。

coroutine countUp apply {{} {  # <<< launch coroutine for asynchronous state
    global value
    for {set value 0} {$value < 10} {incr value} {
        after 1000 [info coroutine]; yield; # <<< suspend coro for 1 second
        puts "Number: $value"
    }
}}

(不用协程也可以写这段代码,但是代码不太清楚。)

当您需要进度条时,建议您使用ttk::progressbar 正确的用户视觉提示,等等。

命令

after 1000; # delay

只是阻塞并且不允许 Tcl/Tk 进行任何处理。 由于你的程序还没有进入事件循环,所以什么都不会显示。

在某些情况下,最终替换“after”命令的任何代码都会有延迟,从而允许屏幕更新。

# don't put the bind inside the for loop
# is this even needed?
bind . "$btn invoke"

set ::waitvar 0
for {set value 0} {$value < 10} {incr value} {  
   # a simplistic solution for purposes of this example
   # vwait cannot be nested, and should be used very carefully or not at all.
   after 1000 set ::waitvar 1
   vwait ::waitvar
   set ::waitvar 0
}

更好的方法更像是:

  proc updateScale { v } {
     global value
     set value $v
  }

  proc checkProgress { } {
     # query the progress of the task and see how far along it is
     set progress [queryProgress]
     updateScale $progress
     after 1000 [list checkProgress]
  }

  # schedule a call to checkProgress to start things off.
  after 1000 [list checkProgress]
  # and now the program enters the event loop

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM