Create a stopwatch using python

12 Jul, 2021
This article focus on creating a stopwatch using Tkinter in python Tkinter : Tkinter is the standard GUI library for Python. Python when combined with Tkinter provides a fast and easy way to create GUI applications. Tkinter provides a powerful object-oriented interface to the Tk GUI toolkit. It’s very easy to get started with Tkinter, here are some sample codes to get your hands on Tkinter in python.  
Python3
# Python program to create a# a new window using Tkinter# importing the required librariesimport tkinter # creating a object 'top' as instance of class Tktop = tkinter.Tk() # This will start the blank windowtop.mainloop()
Output:  
 
Creating a Stopwatch Using Python | GeeksforGeeks - YouTubeGeeksforGeeks529K subscribersCreating a Stopwatch Using Python | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 47:27•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=On8dNuKo4Dw" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Creating Stopwatch using Tkinter
Now lets try to create a program using Tkinter module to create a stopwatch. A stopwatch is a handheld timepiece designed to measure the amount of time elapsed from a particular time when it is activated to the time when the piece is deactivated. A large digital version of a stopwatch designed for viewing at a distance, as in a sports stadium, is called a stop clock. In manual timing, the clock is started and stopped by a person pressing a button. In fully automatic time, both starting and stopping are triggered automatically, by sensors. Required Modules: We are only going to use Tkinter for creating GUI and no other libraries will be used in this program.Source Code:  
Python3
# Python program to illustrate a stop watch# using Tkinter#importing the required librariesimport tkinter as Tkinterfrom datetime import datetimecounter = 66600running = Falsedef counter_label(label):    def count():        if running:            global counter               # To manage the initial delay.            if counter==66600:                            display="Starting..."            else:                tt = datetime.fromtimestamp(counter)                string = tt.strftime("%H:%M:%S")                display=string               label['text']=display   # Or label.config(text=display)               # label.after(arg1, arg2) delays by             # first argument given in milliseconds            # and then calls the function given as second argument.            # Generally like here we need to call the             # function in which it is present repeatedly.            # Delays by 1000ms=1 seconds and call count again.            label.after(1000, count)             counter += 1       # Triggering the start of the counter.    count()        # start function of the stopwatchdef Start(label):    global running    running=True    counter_label(label)    start['state']='disabled'    stop['state']='normal'    reset['state']='normal'   # Stop function of the stopwatchdef Stop():    global running    start['state']='normal'    stop['state']='disabled'    reset['state']='normal'    running = False   # Reset function of the stopwatchdef Reset(label):    global counter    counter=66600       # If rest is pressed after pressing stop.    if running==False:              reset['state']='disabled'        label['text']='Welcome!'       # If reset is pressed while the stopwatch is running.    else:                       label['text']='Starting...'   root = Tkinter.Tk()root.title("Stopwatch")   # Fixing the window size.root.minsize(width=250, height=70)label = Tkinter.Label(root, text="Welcome!", fg="black", font="Verdana 30 bold")label.pack()f = Tkinter.Frame(root)start = Tkinter.Button(f, text='Start', width=6, command=lambda:Start(label))stop = Tkinter.Button(f, text='Stop',width=6,state='disabled', command=Stop)reset = Tkinter.Button(f, text='Reset',width=6, state='disabled', command=lambda:Reset(label))f.pack(anchor = 'center',pady=5)start.pack(side="left")stop.pack(side ="left")reset.pack(side="left")root.mainloop()
Output:  
 
 
This article is contributed by Subhajit Saha. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
kiran_online
sweetyty
sooda367
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Python OOPs Concepts