An Introduction to Interactive Programming in Python 第四周 计时器
程序员文章站
2022-07-14 10:05:25
...
计时器,简单的逻辑使用
# template for "Stopwatch: The Game"
import simplegui
# define global variables
A = 0
B = 0
C = 0
D = 0
E = 0
F = 0
status = False
# define helper function format that converts time
# in tenths of seconds into formatted string A:BC.D
#def format(t):
# define event handlers for buttons; "Start", "Stop", "Reset"
def timer_handler2(): #stop
global A,B,C,D,E,F,status
timer.stop()
if status:
E += 1
if D == 0 and (C == 0 or C == 5):
F += 1
status = False
def timer_handler1(): #start
global status
timer.start()
status = True
def timer_handler3(): #reset
global A,B,C,D,E,status
A = 0
B = 0
C = 0
D = 0
E = 0
F = 0
status = True
timer.stop()
# define event handler for timer with 0.1 sec interval
def sec():
global A,B,C,D
D += 1
if D == 10:
C += 1
D = 0
if C == 10:
B += 1
C = 0
if B == 10:
A += 1
B = 0
return A,B,C,D
# define draw handler
def draw(canvas):
canvas.draw_text(str(A)+":"+str(B)+str(C)+"."+str(D),[50,100],36,"white")
canvas.draw_text(str(E)+"/"+str(F),[150,20],20,"green")
# create frame
frame = simplegui.create_frame("",200,200)
# register event handlers
frame.set_draw_handler(draw)
timer = simplegui.create_timer(100,sec)
frame.add_button("Start",timer_handler1,200)
frame.add_button("Stop",timer_handler2,200)
frame.add_button("reset",timer_handler3,200)
# start frame
frame.start()
#http://www.codeskulptor.org/#user45_AwtefHLPHW_0.py