An Introduction to Interactive Programming in Python 第四周 计时器

计时器,简单的逻辑使用

在这里插入图片描述

# 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值