stopwatch using python

make a stopwatch using python

Make a Stopwatch Using Python

make a stopwatch using python

Hello coders, welcome to the codewithrandom blog. It’s time to code in Python. In this article, we create a stopwatch using Tkinter in python Programming with Complete Source Code. In Python, a stopwatch is typically implemented using the time module, which provides functions to measure time.  A stopwatch in Tkinter is a  (GUI) widget that allows users to measure the amount of time elapsed between the start and stop events.

Tkinter:

Tkinter is a built-in Python library used for creating graphical user interfaces (GUIs). It provides a set of tools and widgets to create windows, buttons, labels, menus, and other GUI elements. Tkinter is a built-in Python GUI library that provides a range of GUI widgets, including the stopwatch, which can be used to create desktop applications with a graphical interface.

Prerequisites

Creating a Stopwatch using Python Tkinter

step 1: open any python code Editor.

step 2: Importing the Required Modules.

import tkinter as Tkinter
from datetime import datetime

step 3: Copy the code for the Stopwatch using  Python, which I provided Below in this article, and save it in a file named “main.py” (or any other name you prefer).

  1.  Run this  python file  main.py to start the stopwatch.

 

Complete Source Code For the stopwatch using Python👇👇👇

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!'
            else:
                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. 
    count()
    

# start function of the stopwatch 
def Start(label):
    global running
    running = True
    counter_label(label)
    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. 
    else:
        label['text'] = '00:00:00'


root = Tkinter.Tk()
root.title("Stopwatch")

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

Running the Stopwatch

We can now run our stopwatch function by executing the Python script in our IDE. When we run this code , it will prompt us to press start button to start the stopwatch. It will then prompt us to press stop button to stop the stopwatch.

make a stopwatch using python

make a stopwatch using python

Conclusion

In this article , we learned how to create a stopwatch using Python. We used the time module to measure the amount of time elapsed between the start and stop events. We created a function to calculate the amount of time elapsed and used the input() function to prompt the user to start and stop the stopwatch. By following these simple steps, you can create your own stopwatch using Python.

Hope you enjoyed building with us! Visit our homepage and you get lot’s of projects💝.

Try more python  coding project such as 👇👇👇

Create Your Own Snake Game using Python(Click Here to read this article )

Make Your Own Brick Breaker Game Using Python ( Click Here to read this article)

Build a Hangman Game Using Python programming ( Click Here to read this article )

Make a rock, paper, scissors game in Python ( Click Here )

#stopwatch using Python

 

 

 

 

 



Leave a Reply