In this tutorial, I will show you how to create a stopwatch using Tkinter.
A Stopwatch is a handheld timepiece designed to count the time. It is two types Manual and Automatic.
We can create a stopwatch by following a few steps:
- At first import the Tkinter library for creating this amazing Stopwatch.
- Then we define a class for stopwatch and pass frame because whatever function we applied here all those should act upon the Tkinter window.
- After that we define the init function and give a window parameter. And we know that self is automatically invoked. Then initialize the super method with the help of the init method.
- Then we need to define several variables used in the whole program and we need to pack the method for all of that which we need to use all things are stored in Tkinter window.
- Then declare a root variable for storing the Tkinter window. Then we need an object for our class and the object holds the window via class.
- After that take the feature method and define stopwatchs features like Buttons is basically START, STOP, RESTART AND QUIT . In this method we will define how look the stopwatch,what will be the text, title, button, font, foreground and background colour .
- Then create the START button function. In this when running condition is not false when it get second then it will be true then the stopwatch get start.
- Then make the STOP button. In this we can use after after_cancel method stop the counting.
- Then make the RESTART button. In this we need to make false the runnig situation and initialize thr hours, minutes, second is 0.
- And the change method is basically set the time like- second, minute, and hour incrementing +1 and second and minutes count upto 60.
- Then the total time means hours minutes ans seconds are count indivisually 0 to 9 by if else statement.
- Then the whole function we concatenate by using concatenator.
import tkinter as stopwatch class sw(stopwatch.Frame): def __init__(self,window=None): super().__init__(window) self.window=window self.new_time= '' self.running= False self.hours= 0 self.minutes= 0 self.seconds= 0 self.pack() self.features() def features(self): self.stop_watch=stopwatch.Label(self,text='00:00:00',background="White",foreground='Black',font=("arial",100,"bold")) self.stop_watch.pack() self.start_button= stopwatch.Button(self,text='START',height=6,width=9,font=('arial',20,'bold'),background='pink',command=self.start) self.start_button.pack(side=stopwatch.LEFT) self.stop_button= stopwatch.Button(self,text='STOP',height=6,width=9,font=('arial',20,'bold'),background='orange',command=self.stop) self.stop_button.pack(side=stopwatch.LEFT) self.restart_button= stopwatch.Button(self,text='RESTART',height=6, width=9,font=('arial',20,'bold'),background='yellow',command=self.restart) self.restart_button.pack(side=stopwatch.LEFT) self.quit_button= stopwatch.Button(self,text="QUIT",height=6,width=9,font=('arial',20,'bold'),background='red',command=self.window.quit) self.quit_button.pack(side=stopwatch.LEFT) self.window.title('STOP WATCH') def start(self): if not self.running: self.stop_watch.after(1000) self.change() self.running = True def stop(self): if self.running: self.stop_watch.after_cancel(self.new_time) self.running = False def restart(self): if self.running: self.stop_button.after_cancel(self.new_time) self.running = False self.hours,self.minutes,self.seconds= 0,0,0 self.stop_watch.config(text="00:00:00") def change(self): self.seconds += 1 if self.seconds == 60: self.minutes +=1 self.seconds = 0 if self.minutes == 60: self.hours += 1 self.minutes = 0 total_hours= f"{self.hours}" if self.hours > 9 else f'0{self.hours}' total_minutes= f"{self.minutes}" if self.minutes > 9 else f'0{self.minutes}' total_seconds= f"{self.seconds}" if self.seconds > 9 else f'0{self.seconds}' self.stop_watch.config(text=total_hours + ':' + total_minutes + ':' + total_seconds) self.new_time = self.stop_watch.after(1000,self.change) root=stopwatch.Tk() obj=sw(window=root) obj.mainloop()
Output: