Create a stopwatch using python

A stopwatch is used to measure the time interval between two events, usually in seconds to minutes. It has various usage like in sports or measuring the flow of heat, current etc in an industrial setup. Python can be used to create a stopwatch by using its tkinter library.

This library provides GUI features to create a stopwatch showing the Start, Stop and Reset options. The key component of the program is using the label.after() method of tkinter.

Syntax

label.after(ms, function=None)

Parameters:

  • ms ? Time in milliseconds
  • function ? Callback function to execute after the specified time

Example

In the below program, we use this method as the key component and design a widget showing the GUI features in the stopwatch ?

import tkinter as tk

count = -1
run = False

def update_timer(label):
    def tick():
        if run:
            global count
            # Just before starting
            if count == -1:
                display = "Starting"
            else:
                display = str(count)
            label['text'] = display
            # Increment the count after every 1 second
            label.after(1000, tick)
            count += 1
    tick()

# Start function
def start_timer(label):
    global run
    run = True
    update_timer(label)
    start_btn['state'] = 'disabled'
    stop_btn['state'] = 'normal'
    reset_btn['state'] = 'normal'

# Stop function
def stop_timer():
    global run
    start_btn['state'] = 'normal'
    stop_btn['state'] = 'disabled'
    reset_btn['state'] = 'normal'
    run = False

# Reset function
def reset_timer(label):
    global count
    count = -1
    if run == False:
        reset_btn['state'] = 'disabled'
        label['text'] = 'Welcome'
    else:
        label['text'] = 'Start'

# Create main window
root = tk.Tk()
root.title("PYTHON STOPWATCH")
root.minsize(width=300, height=200)

# Create label to display time
time_label = tk.Label(root, text="Welcome", fg="blue", 
                     font="Times 25 bold", bg="white")
time_label.pack(pady=20)

# Create buttons
start_btn = tk.Button(root, text='Start', width=25, 
                     command=lambda: start_timer(time_label))
stop_btn = tk.Button(root, text='Stop', width=25, state='disabled', 
                    command=stop_timer)
reset_btn = tk.Button(root, text='Reset', width=25, state='disabled', 
                     command=lambda: reset_timer(time_label))

start_btn.pack(pady=5)
stop_btn.pack(pady=5)
reset_btn.pack(pady=5)

root.mainloop()

How It Works

The stopwatch uses three main functions:

  • Start ? Begins the timer and disables the start button
  • Stop ? Pauses the timer without resetting the count
  • Reset ? Resets the counter to initial state

The after()

Stopwatch States

Welcome Initial State Count: -1 Running Timer Active Count: 0,1,2... Stopped Timer Paused Count: Preserved Reset Back to Start Count: -1 Start Stop Reset Complete

Key Features

  • Real-time Updates ? Timer updates every second using after()
  • Button State Management ? Buttons are enabled/disabled based on stopwatch state
  • Global Variables ? count and run track timer state
  • GUI Layout ? Clean interface with label and three buttons

Conclusion

This Python stopwatch uses tkinter's after() method to create a simple timer application. The program demonstrates GUI programming concepts like event handling, state management, and scheduled function execution.

Updated on: 2026-03-15T17:11:26+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements