Python - Digital Counting Timer

In this tutorial we will create a Digital Counting Timer using Python. This code will start the timer when user click the start button. The code use tkinter module to create a layout and widgets that can call python functions. When a function is called it the application will automatically start counting decremented when it reach zero value it will launch a new window form. We will be using Python because it has a design philosophy which emphasizes code readability. That's why python is very easy to use especially for beginners who just started programming. It is very easy to learn the syntax emphasizes readability and it can reduces time consuming in developing..

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 *

Setting up the Main Frame

After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor.
  1. #==================================MAIN FRAME=========================================
  2. root = Tk()
  3. root.title("Python - Digital Counting Timer")
  4. width = 400
  5. height = 300
  6. screen_width = root.winfo_screenwidth()
  7. screen_height = root.winfo_screenheight()
  8. x = (screen_width/2) - (width/2)
  9. y = (screen_height/2) - (height/2)
  10. root.geometry("%dx%d+%d+%d" % (width, height, x, y))
  11. root.resizable(0, 0)

Designing the Layout

After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor.
  1. #=======================================FRAMES====================================================
  2. Top = Frame(root, width=400, bd=1, relief=SOLID)
  3. Top.pack(side=TOP)
  4. TextField = Frame(root)
  5. TextField.pack(side=TOP)
  6. BtnGroup = Frame(root, width=400)
  7. BtnGroup.pack(side=TOP)
  8.  
  9. #=======================================LABEL WIDGETS=============================================
  10. lbl_title = Label(Top, width=400, text="Python - Digital Counting Timer", font=('times new roman', 16))
  11. lbl_title.pack(fill=X)
  12. lbl_text = Label(TextField, text="Christmas Countdown", font=('arial', 24))
  13. lbl_text.pack(fill=BOTH, pady=50)
  14.  
  15. #=======================================Button WIDGETS=============================================
  16. btn_start = Button(BtnGroup, text="Start Timer", command=StartTimer)
  17. btn_start.pack()

Creating the Main Function

This is where the code that contains the main functions. This code will start the timer when the button is clicked. To do that just copy and write these blocks of code.
  1. #=======================================METHODS===================================================
  2.  
  3. def Timer(count):
  4. lbl_text['text'] = count
  5. if count > 0:
  6. root.after(1000, Timer, count-1)
  7. elif(count == 0):
  8. global NewForm
  9. NewForm = Toplevel()
  10. NewForm.title("Python - Digital Counting Timer")
  11. width = 500
  12. height = 300
  13. screen_width = root.winfo_screenwidth()
  14. screen_height = root.winfo_screenheight()
  15. x = (screen_width/2) - (width/2)
  16. y = (screen_height/2) - (height/2)
  17. NewForm.geometry("%dx%d+%d+%d" % (width, height, x, y))
  18. NewForm.resizable(0, 0)
  19. lbl_blast = Label(NewForm, text="MERRY CHRISTMAS!", font=('arial', 30))
  20. lbl_blast.pack(fill=BOTH, pady=50)
  21. btn_back = Button(NewForm, text="Back", font=('arial', 16), command=BackBtn)
  22. btn_back.pack(side=TOP)
  23.  
  24.  
  25. def StartTimer():
  26. Timer(10)
  27. btn_start.config(state=DISABLED)
  28.  
  29.  
  30. def BackBtn():
  31. NewForm.destroy()
  32. btn_start.config(state=NORMAL)
  33. lbl_text.config(text="10")

Initializing the Application

After finishing the function save the application as index.py. This function will run the code and check if the main application is initialized properly. To do that copy the code below and paste it inside the IDLE text editor.
  1. #=======================================INITIALIZATION=============================================
  2. if __name__ == '__main__':
  3. root.mainloop()
There you have it we just created a Digital Counting Timer 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!!

Add new comment