Generally, we are all aware of the stopwatch. There is a possible way to create a stopwatch in python. Do you know how it is? We understand that the datetime module is already available in python. Making use of that module, we can create a stopwatch in python. We hope this article is going to be amazing. You can use this as a primary-level mini project in python. Let us get start.
We will learn to design a stopwatch in two different ways. They are:
- Using Tkinter
- Without using Tkinter
Tkinter is a package that is very much useful in python. It is the standard python interference to GUI tool kit. If you want to check whether the Tkinter is correctly installed on your system, type this command line on the terminal.
python -m tkinter
It will open some simple Tk interference to let you know that the Tkinter is properly installed. Now let us make use of the Tkinter module to design a stopwatch in python.
Why datetime module is important to a create stopwatch in python?
The DateTime module provides time objects that are similar to the Time objects. A date in Python is not an object, but we can import a module “datetime” to get a date and time. By using the date time and Tkinter module, we are going to design a stopwatch in python.
Code to create a simple stopwatch in python without Tkinter
Import time module to get the time objects. Create a while loop. Inside while loop creates try block that has some set of codes to run the stopwatch. While we press to enter, the stopwatch gets started and run until the user enters ctrl+c. After pressing enter user has to understand that stopwatch has begun. So that we give a print statement as stopwatch has started. We are creating another while loop to return the time elapsed during the run time.
import time
while True:
try:
input("Press Enter to continue and ctrl+C to exit the stopwatch")
start_time=time.time()
print("Stopwatch has started")
while True:
print("Time elapsed:",round(time.time()-start_time,0),'secs',end='\n')
time.sleep(1)
Create an except block; when the user hits the key ctrl+c, it will come to the except block and return the statements. In except block we have given like Timer has stopped. After the timer stopped, we had to display the total time elapsed. So that we are giving another print statement to show the whole time elapsed.
except KeyboardInterrupt:
print("Timer has stopped")
end_time=time.time()
print("The time elapsed:",round(end_time-start_time,2),'secs')
break
Output
Press Enter to continue and ctrl+C to exit the stopwatch Stopwatch has started Time elapsed: 0.0 secs Time elapsed: 1.0 secs Time elapsed: 2.0 secs Time elapsed: 3.0 secs Time elapsed: 4.0 secs Time elapsed: 5.0 secs Time elapsed: 6.0 secs Time elapsed: 7.0 secs Time elapsed: 8.0 secs Time elapsed: 9.0 secs Time elapsed: 10.0 secs Time elapsed: 11.0 secs Timer has stopped The time elapsed: 12.45 secs
Code with explanation to create a stopwatch in python with Tkinter
We need to import the Tkinter module to create a stopwatch in python. Next to import the datetime module to get a time. Declaring count as global and initializing count as zero.
from tkinter import *
import sys
import time
global count
count =0
Create a class named stopwatch in python code. After creating a class, create a function to perform a reset operation. This function will reset the timing as 00:00:00, which is similar to restart the stopwatch.
class stopwatch():
def reset(self):
global count
count=1
self.t.set('00:00:00')
Next, create a function named start to start the stopwatch. This function is helpful to start the stopwatch.
def start(self):
global count
count=0
self.timer()
We need a stop function to stop the stopwatch. So we need to create another function named stop to perform the stop operation in the stopwatch.
def stop(self):
global count
count=1
Always create an exit button while developing some applications. That will make a good impression on your application. So we need to create a function named close to perform the exit operation. While we click this exit button, it will come out of the application.
def close(self):
self.root.destroy()
Next, create a timer function to run the stopwatch. I hope you have seen that I have called a function as self.timer(). So after seeing that statement, the program will call this function and perform the task whatever is given in 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)
After this, we create an init function in that function, creating a button for all the operations. And also, this function will call all the functions. To make our stopwatch better, we are setting some background colors for the texts.
def __init__(self):
self.root=Tk()
self.root.title("Stop Watch")
self.root.geometry("600x200")
self.t = StringVar()
self.t.set("02:15:06")
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=("green"))
self.bt2 = Button(self.root,text="Stop",command=self.stop,font=("Times 12 bold"),bg=("red"))
self.bt3 = Button(self.root,text="Reset",command=self.reset,font=("Times 12 bold"),bg=("orange"))
self.bt4 = Button(self.root, text="Exit", command=self.close,font=("Times 12 bold"),bg=("yellow"))
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='black')
self.root.mainloop()
a=stopwatch()
Entire Code to Create Stopwatch in 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")
self.root.geometry("600x200")
self.t = StringVar()
self.t.set("02:15:06")
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=("green"))
self.bt2 = Button(self.root,text="Stop",command=self.stop,font=("Times 12 bold"),bg=("red"))
self.bt3 = Button(self.root,text="Reset",command=self.reset,font=("Times 12 bold"),bg=("orange"))
self.bt4 = Button(self.root, text="Exit", command=self.close,font=("Times 12 bold"),bg=("yellow"))
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='black')
self.root.mainloop()
a=stopwatch()
Output
FAQs on Designing a Stopwatch in Python
datetime module and Tkinter are the necessary modules to create a stopwatch.
No, it includes only hours, minutes, and seconds.
Conclusion
In this article, we have entirely learned to create a stopwatch in python. This article will be beneficial for all python beginners. Learn this completely. Try to solve the code on your own. In case of any queries, do let us know in the comment box. We will be here for you. Learn python and shine!