Coursera 中 对 timer 的牛刀小试
详情请见https://www.coursera.org/learn/interactive-python-1/lecture/nnSp1/timers
#import modules
import simplegui
import random
#global state
message = "Hello,world!"
position = [50,50]
width = 500
height = 500
interval = 2000
#Handler for update
def update(text):
global message
message = text
#Handler for timer
def tick():
x = random.randrange(0,width)
y = random.randrange(0,height)
position[0] = x
position[1] = y
#Handler for draw
def draw(canvas):
canvas.draw_text(message,position,25,'Red')
#Create frame
frame = simplegui.create_frame('Testing',width,height)
timer = simplegui.create_timer(interval,tick)
frame.set_draw_handler(draw)
text = frame.add_input('message',update,50)
# Start frame and timer
frame.start()
timer.start()