简体   繁体   中英

how make control bar to loop for in Tcl/Tk

I want help from you to create a way to be able to control a bond:

  • Play | Pause | Stop

Only I haven't been able to make a script with the Play & Pause method. Just Stop is too easy, with 'break' method.

Sample code:

set waitvar 0

for {set i 0} {$i < 100} {incr i} {
  puts "$i"
  after 1000 set waitvar
  waitvar vwait 
  set waitvar 0
}

If anyone can help me with some didactic example in Tcl/Tk.

I appreciate it, since now.

If anyone who can give an example is Tcl:

Do with switch . Something like that..

set pause "pausing"

switch pausing 
  $play {expr {1}} 
  $pause {expr {2}} 
  $stop {expr {3}}

Now if who is to give some example in Tk:

Make the call through buttons for easy understanding.

It is not mandatory to make examples of the two, I just leave as a suggestion one of the two - Tcl or Tk

ATTENTION - For the answer to this question, I need it to be given for version Tcl/Tk 8.5

In 8.5 and before, you're best off using asynchronous programming. This requires a very different approach to the one you're using (which is close to what you can do in 8.6 onwards with coroutines). Here are sketches of the procedures you need; the easiest way to implement pausing is by just not incrementing the state variable while paused.

proc UpdateWaitvar {} {
    global waitvar callback paused
    if {$paused} {
        set callback [after 100 UpdadteWaitvar]
    } elseif {[incr waitvar] < 100} {
        set callback [after 1000 UpdateWaitvar]
    }
}

proc CancelWait {} {
    global callback
    after cancel $callback
}

proc StartWait {} {
    global waitvar callback paused
    set waitvar 0
    set paused 0
    set callback [after 1000 UpdateWaitvar]
}

proc TogglePause {} {
    global paused
    set paused [expr {! $paused}]
}

The awkward bit about this is that you have have your “loop” written as chunks of code that you run between the parts where you pause and let other things happen, which doesn't feel like a natural way to work.

Binding the waitvar variable to whatever bits of GUI show the progress is left up to you.


The 8.6 Method

With 8.6, you can use coroutines instead.

proc StartWait {} {
    coroutine waiter apply {{} {
        global waitvar paused callback
        set paused 0
        for {set waitvar 0} {$waitvar < 100} {incr waitvar} {
            set callback [after 1000 [info coroutine]]
            while {$paused} {
                set callback [after 100 [info coroutine]]
            }
        }
    }}
}

proc CancelWait {} {
    global callback
    after cancel $callback
    rename waiter {}
}

# Also same TogglePause as before; it is, after all, just setting the right variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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