How To Create A Stopwatch In Python

In this tutorial, we will be going through a fun program to create a stopwatch in python. For this, we will be using time.time() function from the time module.
A stopwatch essentially tells the time elapsed from start to stop.

time.time() in Python

We will be using time.time() function from the time module. Documentation is here.
time.time() function keeps the track of the number of seconds passed from the time the day began i.e. the epoch 00:00.
To use this function we will first import the time module into our code.

import time

Time to create a stopwatch using Python

  1. We will import time module.
  2. The user will press Enter to start the stopwatch. At this point start_time is set using time.time(). So, at this point, start_time has the number of seconds passed since epoch when the clock is started.
  3. Now, the clock will run in the background.
  4. Now the user will press Enter again for stopping the stopwatch. At this point end_time is set using time.time(). So, at this point, end_time has the number of seconds passed since epoch when the clock is stopped.
  5. So, the time lapse can be calculated using the difference between end_time and start_time.
  6. time_lapsed has the value in seconds. We want the output in hours, minutes and seconds. To do this we will use the user-defined function time_convert().
  7. time_convert() will convert seconds into minutes by dividing the number of seconds by 60 and then the number of minutes divided by 60 is the number of hours.
  8. We are printing the Lapsed time also from inside time_convert().
import time

def time_convert(sec):
  mins = sec // 60
  sec = sec % 60
  hours = mins // 60
  mins = mins % 60
  print("Time Lapsed = {0}:{1}:{2}".format(int(hours),int(mins),sec))

input("Press Enter to start")
start_time = time.time()

input("Press Enter to stop")
end_time = time.time()

time_lapsed = end_time - start_time
time_convert(time_lapsed)

Output

If 140 seconds have passed then the output will look like:

Time Lapsed = 0:2:20

So, here it is.  A very simple program to create a Stopwatch in Python.

Also read,

Create Hangman game in python

Find out the future date with Python

7 responses to “How To Create A Stopwatch In Python”

  1. Choi says:

    Good day sir! Thankyou for this code you provided. It is very helpful.
    But my problem here is that, I want to add a simple push button switch to turn on the stopwatch and then stop it again with it. But I dont know when to insert or even enter a code that will make the system work.

  2. Tyler says:

    How would I make it so I can see the timer while it is going?

    • Javier says:

      Hi Tyler, I was able to do so with this, but still working on it 🙂
      Hope it helps

      while True:
      start_time=time.time()
      while True:
      end_time=time.time()
      print(stopTime-startTime)
      time.sleep(.1)

  3. Alyssa says:

    Is there a way to save the times to view later? like when you open the program back up, it prints the last stopwatch times?

    • Los Skillos says:

      Yes There is, you can save the results into a file
      Here is one of many ways how to do it: (It’ll work only on windows tho)

      import os
      import time
      exist = os.system(“if exist stopwatch.txt echo 1”)
      if exist == “1”:
      lastsec = os.system(“type stopwatch.txt”)
      lastsec = int(lastsec)
      os.system(“del stopwatch.txt”)
      print(“Last stopwatch:”)
      time_convert(lastsec)

      def time_convert(sec):
      mins = sec // 60
      sec = sec % 60
      hours = mins // 60
      mins = mins % 60
      print(“Time Lapsed = {0}:{1}:{2}”.format(int(hours),int(mins),sec))
      input(“Press Enter to start”)
      start_time = time.time()
      input(“Press Enter to stop”)
      end_time = time.time()
      time_lapsed = end_time – start_time
      save = time_lapsed
      time_convert(time_lapsed)
      os.system(“echo %d > stopwatch.txt” % (int(save)))

  4. aaron says:

    here is my solution:

    import tkinter as Tkinter
    from datetime import datetime
    import win32gui, win32con
    the_program_to_hide = win32gui.GetForegroundWindow()
    win32gui.ShowWindow(the_program_to_hide , win32con.SW_HIDE)

    running = False

    h = 0
    m = 0
    s = 0

    def counter_label(label):
    global counter, h, m, s
    def count():
    global counter, h, m, s
    if running:

    s_h = h
    s_m = m
    s_s = s

    if h < 10:
    s_h = "0" + str(h)
    if m < 10:
    s_m = "0" + str(m)
    if s = 60:
    s = 0
    m += 1
    if m >= 60:
    m = 0
    h += 1

    count()

    def Start(label):
    global running
    running=True
    counter_label(label)
    start[‘state’]=’disabled’
    stop[‘state’]=’normal’
    reset[‘state’]=’normal’

    def Stop():
    global running
    start[‘state’]=’normal’
    stop[‘state’]=’disabled’
    reset[‘state’]=’normal’
    running = False

    def Reset(label):
    global counter, h, m, s
    h = 0
    m = 0
    s = 0
    if running==False:
    string = “Hours: 00”
    string2 = “Minutes: 00”
    string3 = “Seconds: 00”
    display=(string)
    display2=(string2)
    display3=(string3)

    label[‘text’]=display
    label2[‘text’]=display2
    label3[‘text’]=display3

    reset[‘state’]=’disabled’

    root = Tkinter.Tk()
    root.title(“Stopwatch”)
    root.minsize(width=250, height=70)

    start_title1 = “””Hours: 00″””

    start_title2 = “””Minutes: 00″””

    start_title3 = “””Seconds: 00″””

    label = Tkinter.Label(root, text=start_title1, fg=”black”, font=”Verdana 30 bold”, anchor=’w’)
    label.pack(fill=’both’)

    label2 = Tkinter.Label(root, text=start_title2, fg=”black”, font=”Verdana 30 bold”, anchor=’w’)
    label2.pack(fill=’both’)

    label3 = Tkinter.Label(root, text=start_title3, fg=”black”, font=”Verdana 30 bold”, anchor=’w’)
    label3.pack(fill=’both’)

    f = Tkinter.Frame(root)

    start = Tkinter.Button(f, text=’Start’, width=6, command=lambda:Start(label))
    stop = Tkinter.Button(f, text=’Stop’,width=6,state=’disabled’, command=Stop)
    reset = Tkinter.Button(f, text=’Reset’,width=6, state=’disabled’, command=lambda:Reset(label))
    f.pack(anchor = ‘center’,pady=5)
    start.pack(side=”left”)
    stop.pack(side =”left”)
    reset.pack(side=”left”)
    root.mainloop()

Leave a Reply

Your email address will not be published. Required fields are marked *