How to Create Simple Stopwatch Program with Tkinter in Python

How to Create Simple Stopwatch Program with Tkinter in Python

Introduction

In this tutorial we will create a How to Create Simple Stopwatch Program with Tkinter in Python. This tutorial purpose is to help you create simple program where you can track your physical record. This will cover all the basic parts for creating the stopwatch. I will provide a sample program to show the actual coding of this tutorial.

This tutorial will make it easy for you to understand just follow my instruction I provided and you can do it without a problem. This program is useful if you want to track your physical record, you can also set your speed using this stopwatch. I will try my best to give you the easiest way of creating this program Stopwatch. So let's do the coding.

Getting Started:

First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.

Importing Modules

After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python. Then copy code that I provided below and paste it inside the IDLE text editor.
  1. from tkinter import *
  2. import tkinter.messagebox as tkMessageBox
  3. import time

Creating Main Function

This is the main function of the application is. This code will create the stopwatch with a complete functionality. To do this just copy and write these block of codes inside the IDLE text editor.
  1. class StopWatch(Frame):
  2.  
  3. def __init__(self, parent=None, **kw):
  4. Frame.__init__(self, parent, kw)
  5. self.startTime = 0.0
  6. self.nextTime = 0.0
  7. self.onRunning = 0
  8. self.timestr = StringVar()
  9. self.MakeWidget()
  10.  
  11. def MakeWidget(self):
  12. timeText = Label(self, textvariable=self.timestr, font=("times new roman", 50), fg="white", bg="blue")
  13. self.SetTime(self.nextTime)
  14. timeText.pack(fill=X, expand=NO, pady=2, padx=2)
  15.  
  16. def Updater(self):
  17. self.nextTime = time.time() - self.startTime
  18. self.SetTime(self.nextTime)
  19. self.timer = self.after(50, self.Updater)
  20.  
  21. def SetTime(self, nextElap):
  22. minutes = int(nextElap/60)
  23. seconds = int(nextElap - minutes*60.0)
  24. miliSeconds = int((nextElap - minutes*60.0 - seconds)*100)
  25. self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, miliSeconds))
  26.  
  27. def Start(self):
  28. if not self.onRunning:
  29. self.startTime = time.time() - self.nextTime
  30. self.Updater()
  31. self.onRunning = 1
  32.  
  33. def Stop(self):
  34. if self.onRunning:
  35. self.after_cancel(self.timer)
  36. self.nextTime = time.time() - self.startTime
  37. self.SetTime(self.nextTime)
  38. self.onRunning = 0
  39.  
  40. def Exit(self):
  41. result = tkMessageBox.askquestion('System Information', 'Are you sure you want to exit?', icon='warning')
  42. if result == 'yes':
  43. root.destroy()
  44. exit()
  45.  
  46. def Reset(self):
  47. self.startTime = time.time()
  48. self.nextTime = 0.0
  49. self.SetTime(self.nextTime)
  50.  
  51. def Main():
  52. global root
  53.  
  54. root = Tk()
  55. root.title("Stopwatch")
  56. width = 600
  57. height = 200
  58. screen_width = root.winfo_screenwidth()
  59. screen_height = root.winfo_screenheight()
  60. x = (screen_width / 2) - (width / 2)
  61. y = (screen_height / 2) - (height / 2)
  62. root.geometry("%dx%d+%d+%d" % (width, height, x, y))
  63. Top = Frame(root, width=600)
  64. Top.pack(side=TOP)
  65. stopWatch = StopWatch(root)
  66. stopWatch.pack(side=TOP)
  67. Bottom = Frame(root, width=600)
  68. Bottom.pack(side=BOTTOM)
  69. Start = Button(Bottom, text='Start', command=stopWatch.Start, width=10, height=2)
  70. Start.pack(side=LEFT)
  71. Stop = Button(Bottom, text='Stop', command=stopWatch.Stop, width=10, height=2)
  72. Stop.pack(side=LEFT)
  73. Reset = Button(Bottom, text='Reset', command=stopWatch.Reset, width=10, height=2)
  74. Reset.pack(side=LEFT)
  75. Exit = Button(Bottom, text='Exit', command=stopWatch.Exit, width=10, height=2)
  76. Exit.pack(side=LEFT)
  77. Title = Label(Top, text="STOPWATCH", font=("arial", 20), fg="white", bg="blue")
  78. Title.pack(fill=X)
  79. root.config(bg="blue")
  80. root.mainloop()

In the given code we created multiple methods called Start, Stop, SetTime and Updater. In the SetTime we set the timer in minutes, seconds, and milliseconds format with a corresponding formula for getting the value. We then create Start function, this will trigger the Updater to start the timer. The Updater function will continuously run the timer after you start it, by the difference of time.time() and StartTime value. Lastly, the Stop function will immediately top the timer when called.

Initializing the Application

After finishing the function save the application as index.py. This function will run the code and check if the main is initialize properly. To do that copy and write these block code after the main function inside the IDLE text editor.

  1. if __name__ == '__main__':
  2. Main()

Output:


The How to Create Simple Stopwatch Program with Tkinter in Python source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Create Simple Stopwatch Program with Tkinter in Python. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for Python Language

Python Tutorials

Add new comment