...

Countdown Timer

We will use a Turtle graphics library to build a countdown timer that displays the number of days until your event.
As part of this project you will learn:

The Turtle library

What is Turtle library?

In Python, a library is a reusable chunk of code you can include in your programs and use pre-existing functionality.

Turtle is a graphic library for Python. Using Turtle, we can easily draw in a drawing board.

First, we import the turtle module. Then create a window, next we create a turtle object and using the turtle method we can draw on the drawing board.

Next
Please ensure all required fields are filled in
Step 1: Let's start coding
  • In your logged-in trinket account click on New Trinket -> Python

  • Or Open the blank Python template trinket: Open New.

  • Prev Next
    Please ensure all required fields are filled in
    Step 2: Create a window
  • The first step is to import the Turtle module. Place import statements at the top of your code.
  • Next, define the screen and specify its dimension. Type the code as follows:
  • done() command must be the last statement in a turtle graphics program.
  • Let's add the background colour to the screen. Follow the code below:
  • The hideturtle() command will hide the cursor
  • Prev Next
    Please ensure all required fields are filled in
    Step 3: Add an image

    Next, we will replace our plane background with the calendar image.

  • Download the calendar image -> click here
  • Add an image to your trinket account
  • Click on the Image icon -> Image Library button->Upload New Image
  • Select the calendar.gif image that you just downloaded
  • Click Done button
  • We can now set the image instead of the background colour by replacing screen.bgcolor("red") with the following as shown in the picture:
  • Prev Next
    Please ensure all required fields are filled in
    Step 4: Add the date

  • Then we will ask you to enter the date you are looking forward to.
  • We will use input() statements and ask for event name, date, month and year.

    Next, we need to modify our inputs so the programming language understands it is a date. For this, we will use another library called datetime

  • Import this library at the top of your code after turtle import
  • Now let's transfer our inputs to the date format, add this line of code after your inputs

  • The int()function converts a number or a string to its equivalent integer.
  • Int, or an integer in programming, is a whole number, positive or negative, without decimals, of unlimited length.

    Prev Next
    Please ensure all required fields are filled in
    Step 5: Calculate the number of days left

    To calculate how many days are left we also need to know today's date.

  • To find out today's date type the following line of code:
  • So now that we know those two dates, we can calculate the difference as follows:
  • Prev Next
    Please ensure all required fields are filled in
    Step 6: Display on the calendar

    We already knew how many days are left, let's display it on the calendar
    To do it we use the following commands:

  • penup() - get your writing pen up
  • pendown() - put your writing pen down
  • goto(x,y) - go to specific coordinate
  • You can write text on the screen using write() command. This function is used to write text at the current turtle position. Syntax :
    write(text, font=('Arial', 8, 'normal'),align='left')

    Prev Next
    Please ensure all required fields are filled in
    Step 7: Save your work

  • If you don't have a Trinket account, click the down arrow and then click Link. This will give you a link that you can save and come back to later.
  • If you have a Trinket account, you can click Remix to save your own copy of the trinket.
  • Prev Next
    Step 8: Challenge

    Optional: Want more? Try to understand the code and learn more turtle commands on your own. You can also type this code in a new trinket window and see what happens and create your own art.

    You can submit your drawings once again, just add a new trinket url.

    I am waiting to see your artwork!

    Prev Next
    Please ensure all required fields are filled in
    Step 9: Complete and Share

    Submit your final project

    In order to receive your completion certificate all projects should be submited before 6th April 2023



    Copy your Trinket Url

  • Click Share->Link
  • Copy the link to share your code
  • Paste link into the field
  • Prev