
Animation is integral to many parts of GUIs, including both the fancy effects when things change used in window managers, and of course games.   The core of any animation system is a scheme for periodically changing the display while still remaining responsive to the user.   This task demonstrates this.



Task

Create a window containing the string "Hello World! " (the trailing space is significant).

Make the text appear to be rotating right by periodically removing one letter from the end of the string and attaching it to the front.

When the user clicks on the (windowed) text, it should reverse its direction.




package require Tk
set s "Hello World! "
set dir 0
# Periodic animation callback
proc animate {} {
    global dir s
    if {$dir} {
        set s [string range $s 1 end][string index $s 0]
    } else {
        set s [string index $s end][string range $s 0 end-1]
    }
    # We will run this code ~8 times a second (== 125ms delay)
    after 125 animate
}
# Make the label (constant width font looks better)
pack [label .l -textvariable s -font {Courier 14}]
# Make a mouse click reverse the direction
bind .l <Button-1> {set dir [expr {!$dir}]}
# Start the animation
animate