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
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:
- Game in Python Assignment Expert
- Guessing number in python assignment expert
- Make it equal in Python assignment expert
- New Internship for Python Freshers | Apply Now
- Naive Bayes in Machine Learning
- Roti Prata SPOJ Problem Solution – Complete Solution Approach with C++ and Java Code
- Lee Algorithm in Python | Solution to Maze Routing Problem in Python
- Free Python Course | Loved By Over 7.7 Million People
- Top 10 Python Projects for Final year Students
- 10 Tkinter Projects for Beginners
- Miles Per Gallon Python Program
- Get a New Job with Google: Course by Google
- Top 5 Websites To Learn Programming in 2022
- FAQs
- Automate Data Mining With Python
- ASCII Art in Python with art library
- Create and Print a List of Prime Numbers in Python
- Support Vector Machine(SVM) in Machine Learning
- Python Increment By 1
- Vending Machine with Python Code
- I Love You Text in ASCII Art
- Top 5 Youtube Channels to Learn Programming in 2022
- Python Turtle Shapes- Square, Rectangle, Circle
- Python OOP Projects | Source code and example
- Internship at Google: Apply Now | Google STEP Internship
- Python Turtle Commands and All Methods
- Generate QR Code in Python
- Happy Birthday In Binary Code
- Python Developer Invitation For Freshers at CLARIWELLGLOBAL SERVICES: Apply Now
- AES in Python | Encrypt & Decrypt | PyCryptodome
Keywords->python turtle design, turtle, design, 12th class python project, python for kids, 12th class, beautiful turtle designs, python, class 12th, class 12