Python: Simple Stopwatch For Beginners

In this tutorial we will create a Simple Stopwatch Using Python. Python is a widely used advance-level programming language for a general technique to the developer. Beginners find Python a clean syntax and indentation structure based, and it is easy to learn because of its less semi colon problem. So let 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, 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 the Interface This is the interface code for the Python application.The window will automatically generate when the program run. To do that just copy the code below then paste it inside the IDLE text editor.
  1. def Main():
  2. global root
  3.  
  4. root = Tk()
  5. root.title("Sourcecodester")
  6. width = 600
  7. height = 200
  8. screen_width = root.winfo_screenwidth()
  9. screen_height = root.winfo_screenheight()
  10. x = (screen_width / 2) - (width / 2)
  11. y = (screen_height / 2) - (height / 2)
  12. root.geometry("%dx%d+%d+%d" % (width, height, x, y))
  13. Top = Frame(root, width=600)
  14. Top.pack(side=TOP)
  15. stopWatch = StopWatch(root)
  16. stopWatch.pack(side=TOP)
  17. Bottom = Frame(root, width=600)
  18. Bottom.pack(side=BOTTOM)
  19. Start = Button(Bottom, text='Start', command=stopWatch.Start, width=10, height=2)
  20. Start.pack(side=LEFT)
  21. Stop = Button(Bottom, text='Stop', command=stopWatch.Stop, width=10, height=2)
  22. Stop.pack(side=LEFT)
  23. Reset = Button(Bottom, text='Reset', command=stopWatch.Reset, width=10, height=2)
  24. Reset.pack(side=LEFT)
  25. Exit = Button(Bottom, text='Exit', command=stopWatch.Exit, width=10, height=2)
  26. Exit.pack(side=LEFT)
  27. Title = Label(Top, text="Python: Simple Stopwatch For Beginners", font=("arial", 20), fg="white", bg="black")
  28. Title.pack(fill=X)
  29. root.config(bg="black")
  30. root.mainloop()
Creating The Main Function This is the Main Code for the Python application. The code contains class that has a several function that will be called. To do that just copy the code below then paste it inside the IDLE text editor.
  1. class StopWatch(Frame):
  2. # Initialize the Main Function of the Stopwatch
  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. # Create the widget of the Stopwatch Timer
  12. def MakeWidget(self):
  13. timeText = Label(self, textvariable=self.timestr, font=("times new roman", 50), fg="green", bg="black")
  14. self.SetTime(self.nextTime)
  15. timeText.pack(fill=X, expand=NO, pady=2, padx=2)
  16.  
  17. # Continously Update The Time From Counting
  18. def Updater(self):
  19. self.nextTime = time.time() - self.startTime
  20. self.SetTime(self.nextTime)
  21. self.timer = self.after(50, self.Updater)
  22.  
  23. # Set The Value of Time When Is Called
  24. def SetTime(self, nextElap):
  25. minutes = int(nextElap/60)
  26. seconds = int(nextElap - minutes*60.0)
  27. miliSeconds = int((nextElap - minutes*60.0 - seconds)*100)
  28. self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, miliSeconds))
  29.  
  30. # Start The Stopwatch Counting When Button Start Is Clicked
  31. def Start(self):
  32. if not self.onRunning:
  33. self.startTime = time.time() - self.nextTime
  34. self.Updater()
  35. self.onRunning = 1
  36.  
  37. # Stop The Stopwatch Counting When Button Stop Is Clicked
  38. def Stop(self):
  39. if self.onRunning:
  40. self.after_cancel(self.timer)
  41. self.nextTime = time.time() - self.startTime
  42. self.SetTime(self.nextTime)
  43. self.onRunning = 0
  44.  
  45. # Close The Application When Exit Button Is Clicked
  46. def Exit(self):
  47. result = tkMessageBox.askquestion('Sourcecodester', 'Are you sure you want to exit?', icon='warning')
  48. if result == 'yes':
  49. root.destroy()
  50. exit()
  51.  
  52. # Reset The Timer When Reset Button Is Clicked
  53. def Reset(self):
  54. self.startTime = time.time()
  55. self.nextTime = 0.0
  56. self.SetTime(self.nextTime)
  57.  
Initializing the Application This function will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the IDLE text editor.
  1. if __name__ == '__main__':
  2. Main()
There you have it we just created a Simple Stopwatch Using Python. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Tags

Add new comment