Build Stop watch in Python


In this python tutorial, you will learn how you can build a stopwatch in python. The best way to learn python is to build something with it.

The code provided is a well-documented code and is very easy to understand. Go through the code and you will see how to create a stopwatch in python.

python stopwatch

Python code To Build Stop Watch

The best way to unserstand how to build a stop watch in python is to learn what we have used in it. we have used the tkinter module and the datetime module.


You should know

We have used Tkinter for creating a graphical user interface in python, but you can use any of the graphical user interface library in python to build this project.

Recommended Articles:

Python Libraries for DateTime

DateTime package in Python

Python Source code for Building stopwatch:

import tkinter as Tkinter
from datetime import datetime
counter = 0
running = False

def counter_label(label):
    def count():
        if running:
            global counter
			# To manage the intial delay. 
            if counter == 0:
                display = 'Ready!'
                tt = datetime.utcfromtimestamp(counter)
                string = tt.strftime('%H:%M:%S')
                display = string
            label['text'] = display
			# label.after(arg1, arg2) delays by 
			# first argument given in milliseconds 
			# and then calls the function given as second argument. 
			# Generally like here we need to call the 
			# function in which it is present repeatedly. 
			# Delays by 1000ms=1 seconds and call count again. 
            label.after(1000, count)
            counter += 1
	# Triggering the start of the counter. 

# start function of the stopwatch 
def Start(label):
    global running
    running = True
    start['state'] = 'disabled'
    stop['state'] = 'normal'
    reset['state'] = 'normal'

# Stop function of the stopwatch 
def Stop():
    global running
    start['state'] = 'normal'
    stop['state'] = 'disabled'
    reset['state'] = 'normal'
    running = False

# Reset function of the stopwatch 
def Reset(label):
	global counter
	counter = 0
	# If reset is pressed after pressing stop. 
	if not running:
		reset['state'] = 'disabled'
		label['text'] = '00:00:00'
	# If reset is pressed while the stopwatch is running. 
		label['text'] = '00:00:00'

root = Tkinter.Tk()

# Fixing the window size.
root.minsize(width=250, height=70)
label = Tkinter.Label(root, text='Ready!', fg='black', font='Verdana 30 bold')
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)

Related PythonProjects

Summary and Conclusion

In this article, you have learned how to build a stopwatch application for desktop in python. if you have any questions, please leave them in the comment section.

Post a Comment

* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !