Build a Stopwatch Using Python

Ā Build a Stopwatch using PYTHON

Stopwatch Using Python

Today we will learn to make a stopwatch step by step, we all need a stopwatch at a party, so why not make one by coding it in Python today ?This will also enhance our Python skills and we will learn something new.

First we will download Python and then download the latest version of the interpreter on our PC and then import some libraries.

Libraries Name:-

  • sys
  • time
  • Tkinter

Now, let’s start the codeĀ 

Step 1: ImportingĀ  Modules:-

from tkinter import *
import sys
import time

In this line of code, we imported some models like tkinter which is for graphical user interface and SYS which interacts with the system and manages time to show real time.

Build a stopwatch using python

Step 2: Global Variables:-

global count
count = 0

In this line of code,Ā  we will create a global variable count function which works to start from zero and controls the start and stop functionalities.Ā 

Step 3: Define the Stopwatch Class:-

class Stopwatch():
    ...

we will create a class called Stopwatch that will contain methods to (start, stop, reset, and exit) update time to display.Build a Stopwatch Using Python

Step 4: Reset Method:-

def reset(self):
    global count
    count = 1
    self.t.set('00:00:00')

In this line The reset method set the count variable to 1 and reset the display to ’00:00:00′ when the reset button is clicked.Ā 

Step 5: Start Method:-

def start(self):
    global count
    count = 0
    self.timer()

In this line The start method sets the global count variable to 0 and calls the timer method to start the stopwatch when the start button is clicked.

Step 6: Stop Method:-

def stop(self):
    global count
    count = 1

The stop method sets the global count variable to 1, effectively pausing the stopwatch when the stop button is clicked.

Build a Stopwatch Using Python

Step 7: Close Method:-

def close(self):
    self.root.destroy()

The close method destroys the Tkinter window, closing the application when the exit button is clicked.Ā 

Step 8: Timer Method:-

def timer(self):
    ...

The timer method is responsible for updating the display with the spent time. It uses the after method to create a regular timer event, updating the display every second.

Step 9: Initialization:-

def __init__(self):
    ...

In the __init__ method, we initialize the Tkinter windowĀ 

Step 10: Instantiate the Stopwatch Class:-

# Instantiate the Stopwatch class
a = Stopwatch()

Finally, we create an instance of the Stopwatch class, initiating the application.

final OutPut:-

Build a Stopwatch Using Python

Here is full code of Build a Stopwatch Using Python:-

from tkinter import *
import sys
import time
global count
count =0
class stopwatch():
    def reset(self):
        global count
        count=1
        self.t.set('00:00:00')        
    def start(self):
        global count
        count=0
        self.timer()   
    def stop(self):
        global count
        count=1
    def close(self):
        self.root.destroy()
    def timer(self):
        global count
        if(count==0):
            self.d = str(self.t.get())
            h,m,s = map(int,self.d.split(":")) 
            h = int(h)
            m=int(m)
            s= int(s)
            if(s<59):
                s+=1
            elif(s==59):
                s=0
                if(m<59):
                    m+=1
                elif(m==59):
                    m=0
                    h+=1
            if(h<10):
                h = str(0)+str(h)
            else:
                h= str(h)
            if(m<10):
                m = str(0)+str(m)
            else:
                m = str(m)
            if(s<10):
                s=str(0)+str(s)
            else:
                s=str(s)
            self.d=h+":"+m+":"+s           
            self.t.set(self.d)
            if(count==0):
                self.root.after(1000,self.timer)     
    def __init__(self):
        self.root=Tk()
        self.root.title("Stop Watch CodeWithRandom")
        self.root.geometry("600x220")
        self.t = StringVar()
        self.t.set("00:00:00")
        self.lb = Label(self.root,textvariable=self.t,font=("Times 40 bold"),bg="white")
        self.bt1 = Button(self.root,text="Start",command=self.start,font=("Times 11 bold"),bg=("yellow"))
        self.bt2 = Button(self.root,text="Stop",command=self.stop,font=("Times 11 bold"),bg=("blue"))
        self.bt3 = Button(self.root,text="Reset",command=self.reset,font=("Times 11 bold"),bg=("red"))
        self.bt4 = Button(self.root, text="Exit", command=self.close,font=("Times 11 bold"),bg=("white"))
        self.lb.place(x=160,y=10)
        self.bt1.place(x=120,y=90)
        self.bt2.place(x=220,y=100)
        self.bt3.place(x=320,y=90)
        self.bt4.place(x=420,y=100)
        self.label = Label(self.root,text="",font=("Times 30 bold"))
        self.root.configure(bg='green')
        self.root.mainloop()
a=stopwatch()      

OutPut Preview Video:-

We created a stopwatch application using Tkinter in Python. You can also see buttons (Start, Stop, Exit, Rest and Exit) in it. Each button plays its role well.

ADVERTISEMENT

Thanks For Reading My ArticlešŸ«‚

Author:- Ashutosh Mishra

ADVERTISEMENT

Visit my more article = Codewithrandom

ADVERTISEMENT

FAQs:-

ADVERTISEMENT

Can we change its background color?

Build a Stopwatch Using Python

Yes brother, we can change the background color by using this elf.root.configure(bg='green') function.

ADVERTISEMENT

To create a stopwatch in Python, which module do I need?

These are some modules that you will need: tkinter, sys, time , global count

 



Leave a Reply