计时器-rice university
# _*_ coding:utf-8 _*_ # mimi-program of stop watch(秒表) import SimpleGUICS2Pygame.simpleguics2pygame as simplegui # define global variables running=False current_time=0 interval=100 # minites=0 # seconds_one=0 # seconds_ten=0 # miseconds=0 stop_num=0 stop_succ_num=0 # define helper function format that converts time # in tenths of seconds into formatted string A:BC.D def format(t): global current_time,minites,seconds_one,seconds_ten,miseconds t=current_time minites=int(t/600) seconds_ten=int((t-minites*600)/100) seconds_one=int((t-minites*600-seconds_ten*100)/10) miseconds=t-minites*600-seconds_ten*100-seconds_one*10 return str(minites)+':'+str(seconds_ten)+str(seconds_one)+'.'+str(miseconds) # define event handlers for buttons; "Start", "Stop", "Reset" def start(): global running if running is False: timer.start() running=True def stop(): global current_time,minites,seconds_one,seconds_ten,miseconds global running,stop_num,stop_succ_num if running is True: timer.stop() stop_num+=1 if miseconds==0 and current_time!=0: stop_succ_num+=1 running=False #return str(stop_succ_num)+'/'+str(stop_num) def reset(): global current_time,running,stop_num,stop_succ_num timer.stop() current_time=0 stop_num=0 stop_succ_num=0 running=False pass # define event handler for timer with 0.1 sec interval def tick(): global current_time current_time+=interval/100 return current_time # str(minites)+':'+str(seconds_ten)+str(seconds_one)+'.'+str(miseconds) # define draw handler def draw_time(canvas): #global current_time,minites,seconds_one,seconds_ten,miseconds canvas.draw_text(format(current_time),[100,100],34,'red') canvas.draw_text(str(stop_succ_num)+'/'+str(stop_num),[250,20],34,'blue') # create frame frame=simplegui.create_frame('Stop Watch',300,200) # register event handlers frame.add_button('start',start,150) frame.add_button('stop',stop,150) frame.add_button('reset',reset,150) frame.set_draw_handler(draw_time) timer=simplegui.create_timer(interval,tick) # start frame frame.start() #timer.stop() # Please remember to review the grading rubric