Create a Stopwatch Using Python Tkinter

Stopwatch Using Python Tkinter

Introduction

We are all familiar with the stopwatch. In Python, it is possible to create a stopwatch. According to our information, the DateTime module is already available in Python. We can use that module to create a timer in Python. We are hoping that this content will be fantastic. You can use this as a primary-level Python mini-project. Let’s get started.

Let’s code now

Step1. To create a stopwatch in python with Tkinter.

from tkinter import *
import sys
import time
global count
count =0

To make a timer in Python, we must first import the Tkinter package. To obtain a time, import the DateTime module. Declaring count as global and setting it to zero.

Step 2. Creating a Stopwatch Function

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

In Python code, create a stopwatch class. Create a function to conduct a reset action after creating a class. This function resets the timing to 00:00:00, akin to restarting the stopwatch.

Step 3. Creating a Start Function.

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

To start the stopwatch, write a function called start. This feature is useful for starting the stopwatch.

Step 4. Creating a Stop Function.

    def stop(self):
        global count
        count=1

To stop the stopwatch, we need a stop function. To conduct the stop operation in the stopwatch, we must write another function called to stop.

Step 5. Creating a close Function

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

When creating applications, always include an exit button. That will make an excellent first impression on your application. To conduct the exit procedure, we must develop a function called close. When we click the exit button, it will exit the application.

Step 6. Create the timer Function.

    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)     

Create a timer function to run the stopwatch. I hope you saw that I named a function self.timer(). So, when the program sees that sentence, it will call this function and do the task specified in the timer function.

Step 7. Creating a init Function.

    def __init__(self):
        self.root=Tk()
        self.root.title("Stop Watch")
        self.root.geometry("600x200")
        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 12 bold"),bg=("#F88379"))
        self.bt2 = Button(self.root,text="Stop",command=self.stop,font=("Times 12 bold"),bg=("#F88379"))
        self.bt3 = Button(self.root,text="Reset",command=self.reset,font=("Times 12 bold"),bg=("#DE1738"))
        self.bt4 = Button(self.root, text="Exit", command=self.close,font=("Times 12 bold"),bg=("#DE1738"))
        self.lb.place(x=160,y=10)
        self.bt1.place(x=120,y=100)
        self.bt2.place(x=220,y=100)
        self.bt3.place(x=320,y=100)
        self.bt4.place(x=420,y=100)
        self.label = Label(self.root,text="",font=("Times 40 bold"))
        self.root.configure(bg='white')
        self.root.mainloop()
a=stopwatch()     

Following that, we add an init function in this program, as well as a button for each operation. Furthermore, this function will invoke all of the functions. To improve our timer, we’re changing the backdrop colors of the letters.

Source Code to Create a Stopwatch using Python Tkinter

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")
        self.root.geometry("600x200")
        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 12 bold"),bg=("#F88379"))
        self.bt2 = Button(self.root,text="Stop",command=self.stop,font=("Times 12 bold"),bg=("#F88379"))
        self.bt3 = Button(self.root,text="Reset",command=self.reset,font=("Times 12 bold"),bg=("#DE1738"))
        self.bt4 = Button(self.root, text="Exit", command=self.close,font=("Times 12 bold"),bg=("#DE1738"))
        self.lb.place(x=160,y=10)
        self.bt1.place(x=120,y=100)
        self.bt2.place(x=220,y=100)
        self.bt3.place(x=320,y=100)
        self.bt4.place(x=420,y=100)
        self.label = Label(self.root,text="",font=("Times 40 bold"))
        self.root.configure(bg='white')
        self.root.mainloop()
a=stopwatch()      

The output of Stopwatch using Tkinter

output for Stopwatch Using Python Tkinter
Output

In this tutorial, we learned how to create a Stopwatch Using Python Tkinter. This article will be useful for all Python beginners. Learn it completely. Try to solve the code on your own. If you have any questions, please leave them in the comment section. We will be there for you. if you want to learn more python tutorials checkout our site copyassignment.com.


Also Read:


Keywords->python turtle design, turtle, design, 12th class python project, python for kids, 12th class, beautiful turtle designs, python, class 12th, class 12

Share:
Avatar of Vatsal Rakholiya

Author: Vatsal Rakholiya