Create a stopwatch using python
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.
# Python program to create a # a new window using Tkinter # importing the required libraires import tkinter # creating a object 'top' as instance of class Tk top = tkinter.Tk() # This will start the blank window top.mainloop() |
Output:
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 stopclock. 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.
# Python program to illustrate a stop watch # using Tkinter #importing the required libraries import tkinter as Tkinter counter = - 1 running = False def counter_label(label): def count(): if running: global counter # To manage the intial delay. if counter = = - 1 : display = "Starting..." else : display = str (counter) 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 stopwatch def Start(label): global running running = True counter_label(label) start[ 'state' ] = 'disabled' stop[ 'state' ] = 'normal' reset[ 'state' ] = 'normal' # Stop function of the stopwatch def Stop(): global running start[ 'state' ] = 'normal' stop[ 'state' ] = 'disabled' reset[ 'state' ] = 'normal' running = False # Reset function of the stopwatch def Reset(label): global counter counter = - 1 # 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() start = Tkinter.Button(root, text = 'Start' , width = 15 , command = lambda :Start(label)) stop = Tkinter.Button(root, text = 'Stop' , width = 15 , state = 'disabled' , command = Stop) reset = Tkinter.Button(root, text = 'Reset' , width = 15 , state = 'disabled' , command = lambda :Reset(label)) start.pack() stop.pack() reset.pack() root.mainloop() |
Output:
Reference Links
1. Wikipedia – Stopwatch
2. Python docs – tkinter
3. Github code
4. Tutorialspoint – Tkinter
This article is contributed by Subhajit Saha. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.
Recommended Posts:
- Python | Create a stopwatch Using Clock Object in kivy
- Python | Create a stopwatch using clock object in kivy using .kv file
- Create a directory in Python
- Create a Website Alarm Using Python
- Create a watchdog in Python to look for filesystem changes
- Create and Access a Python Package
- Create an empty file using Python
- Create a Weather app using Flask | Python
- How to create an empty class in Python?
- Python | Create checkbox using .kv file
- Python Tkinter | Create LabelFrame and add widgets to it
- Python program to create a dictionary from a string
- Python | Create Box Layout widget using .kv file
- Create virtual environment using venv | Python
- Python | Create a digital clock using Tkinter