Sign InCoursewareNuggetsTutorials CoursesCodePad

Stopwatch

With the Donut Clock task, you've built a clock. Now let's implement a stopwatch using ontimer( ) and onclick( ).

Requirement:

  • The timer changes rapidly, say every 0.01 second.
  • It will stop, and continue, whenever you click on the screen.
import turtle screen = turtle.Screen() screen.setup(600, 120) text_turtle = turtle.Turtle() text_turtle.hideturtle() text_turtle.speed(0) stop = False tick = 0 def update_time(): global tick if tick < 1000 and not stop: tick = tick + 1 text_turtle.clear() text_turtle.write(tick, font=("Arial", 60, "normal")) screen.ontimer(update_time, 10) def handle_click(x, y): global stop if stop: stop = False else: stop = True update_time() screen.onclick(handle_click)

Extra credits:

  • Add Start, Stop, Reset buttons.
  • Make the clock look more realistic.
  • Make a game that challenges your user to hit an exact number (say 100).
import turtle screen = turtle.Screen() screen.setup(600, 120)
© CS Wonders·About·Gallery·Fun Facts·Cheatsheet