Python: Simple Countdown Timer
Submitted by razormist on Friday, May 26, 2017 - 13:53.
In this tutorial we will create a Simple Countdown Timer 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 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.
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.
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.
Creating the Main Function
This where the code that handle the countdown timer. The countdown timer will counting when the bind button is clicked.
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 the code below and paste it inside the IDLE text editor.
- from tkinter import *
- root = Tk()
- root.title("Sourcecodester")
- width = 500
- height = 400
- screen_width = root.winfo_screenwidth()
- screen_height = root.winfo_screenheight()
- x = (screen_width/2) - (width/2)
- y = (screen_height/2) - (height/2)
- root.geometry("%dx%d+%d+%d" % (width, height, x, y))
- root.resizable(0, 0)
- #=======================================FRAMES====================================================
- Top = Frame(root, width=500, bd=1, relief=SOLID)
- Top.pack(side=TOP)
- TextField = Frame(root)
- TextField.pack(side=TOP)
- BtnGroup = Frame(root, width=500)
- BtnGroup.pack(side=BOTTOM)
- #=======================================LABEL WIDGETS=============================================
- lbl_title = Label(Top, width=500, text="Python: Simple Countdown Timer", font=('times new roman', 16))
- lbl_title.pack(fill=X)
- lbl_text = Label(TextField, text="10", font=('arial', 120))
- lbl_text.pack(fill=BOTH)
- #=======================================Button WIDGETS=============================================
- btn_toggle = Button(BtnGroup, text="Start Countdown", command=StartCountdown)
- btn_toggle.pack()
- #=======================================METHODS===================================================
- def countdown(count):
- lbl_text['text'] = count
- if count > 0:
- root.after(1000, countdown, count-1)
- elif(count == 0):
- global NewForm
- NewForm = Toplevel()
- NewForm.title("Sourcecodester")
- width = 500
- height = 300
- screen_width = root.winfo_screenwidth()
- screen_height = root.winfo_screenheight()
- x = (screen_width/2) - (width/2)
- y = (screen_height/2) - (height/2)
- NewForm.geometry("%dx%d+%d+%d" % (width, height, x, y))
- NewForm.resizable(0, 0)
- lbl_blast = Label(NewForm, text="Blast Off!", font=('arial', 50))
- lbl_blast.pack(fill=BOTH, pady=100)
- btn_back = Button(NewForm, text="Reset", font=('arial', 16), command=BackBtn)
- btn_back.pack(side=TOP)
- def StartCountdown():
- countdown(10)
- btn_toggle.config(state=DISABLED)
- def BackBtn():
- NewForm.destroy()
- btn_toggle.config(state=NORMAL)
- lbl_text.config(text="10")
- #=======================================INITIALIZATION=============================================
- if __name__ == '__main__':
- root.mainloop()
Add new comment
- 526 views