欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Coursera——对timer的牛刀小试

程序员文章站 2024-01-05 19:51:46
...

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()