Tcl/Tk Cookbook - Using the Canvas


Part-I - Step2: Animate a canvas object

The after command

Tk command after is used to delay a command for a given number of milliseconds. If there are additional arguments to after beyond the time delay, then after concatenates the additional arguments into a script to be evaluated in the background after the given time delay and after returns immediately.

The return value of after is a unique command identifier of the delayed event. This identifier can be used to access the delayed command, to cancel it for instance.

Usage


proc movie {ta tim} {
  global id
  .mv.c move $ta 1 1
  set id [after $tim movie $ta $tim]

}

The procedure "movie" moves an object (text string in this case) identified by its tag name, one pixel in each of the x and y direction. Then it reschedules itself to be invoked after the given time interval. The global variable "id" is assigned the return value from after each time after arranges for "movie" to be invoked. The procedure will be invoked recursively making the tagged text move diagonally down