stopwatch.py

Created by vnap0v

Created on May 24, 2025

812 Bytes

This implements a simple stopwatch. It uses the time module for the monotonic() function. It also uses kandinsky to put text on the screen at a fixed position and ion to detect key presses. Tested on the calculator using software version 23.2.6.


from time import *
from ion import *
from kandinsky import *

draw_string("Stopwatch",5,10)
draw_string("right arrow key to start",5,30)
draw_string("left arrow key to stop",5,50)
draw_string("down arrow key to reset",5,70)

running=False;first=True
total=0
formatstr="time: {:6.2f}s"
draw_string(formatstr.format(0.0),15,100)
while True:
  #start
  if keydown(KEY_RIGHT)==True:
    if running==False:
      if first==True:  
        first=False
      start=monotonic()
      running=True
  #stop    
  if keydown(KEY_LEFT)==True:
    if running==True:
      running=False
      total=t
  #reset
  if keydown(KEY_DOWN)==True:
    draw_string(formatstr.format(0.0),15,100)
    total=0
  if running==True:
    now=monotonic()
    t=now-start+total  
    draw_string(formatstr.format(t),15,100)
   

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.