DEV Community

loading...

Create a simple pomodoro timer in python

Code_Jedi
Javascript, Node.js, Python, PHP, React and Vue. Coding since 2017
Updated on ・3 min read

Hey👋

If you're looking for a pretty useful yet simple python project, you've come to the right place!


For those who don't know what a pomodoro timer is:

A pomodoro timer is a special type of timer used to increase productivity.
It works something like this:

  • A pomodoro timer will give you ~25-30 minutes to work, then once you've finished working (thus completing 1 pomodoro), it will give you ~10 minutes to rest, then the timer will repeat the process.

To get started with making a pomodoro timer in python, you'll first need to install the "plyer" library by running pip install plyer or pip3 install plyer.

Then import these 2 libraries at the beginning of your python file:

import time
from plyer import notification
Enter fullscreen mode Exit fullscreen mode

Next, define the variable which will represent the amount of pomodoros completed by the user, as well as an indicator that the pomodoro timer has started:

import time
from plyer import notification

count = 0
print("The pomodoro timer has started, start working!")
Enter fullscreen mode Exit fullscreen mode

Now let's make the actual timer!

First, you'll need to put the timer in a while True: loop nested inside an if __name__ == "__main__": statement ( if you're not familiar with it, here's a good explanation: https://stackoverflow.com/questions/419163/what-does-if-name-main-do ):

if __name__ == "__main__":
    while True:
Enter fullscreen mode Exit fullscreen mode

Next, make the first notification function which will notify the user when they've finished a pomodoro:

if __name__ == "__main__":
    while True:
        notification.notify(
            title = "Good work!",
            message = "Take a 10 minute break! You have completed " + str(count) + " pomodoros so far",
        )
)
Enter fullscreen mode Exit fullscreen mode

As you can see, this "notify()" function notifies the user that he/she has finished a pomodoro, and it indicates how many pomodoros the user has finished so far with the "count" variable.


Next, you'll need to create the "notify()" function which will notify the user once his/her 10 minutes of break time are over:

if __name__ == "__main__":
    while True:
        notification.notify(
            title = "Good work!",
            message = "Take a 10 minute break! You have completed " + str(count) + " pomodoros so far",
        )
        notification.notify(
            title = "Back to work!",
            message = "Try doing another pomodoro...",
        )
)
Enter fullscreen mode Exit fullscreen mode

Your pomodoro timer is almost finished!

The only things left to do are:

  • Set timeouts between the "notify()" functions.
  • Increment the "count" variable every time the user finishes a pomodoro.
if __name__ == "__main__":
    while True:
        time.sleep(1800)
        count += 1
        notification.notify(
            title = "Good work!",
            message = "Take a 10 minute break! You have completed " + str(count) + " pomodoros so far",
        )
        time.sleep(600)
        notification.notify(
            title = "Back to work!",
            message = "Try doing another pomodoro...",
        )
Enter fullscreen mode Exit fullscreen mode

Let me explain:

  • The first "time.sleep()" function waits 30 minutes(1800 seconds) before displaying the first notification, then before displaying the notification, the "count" variable is incremented since 30 minutes have passed, meaning that the user has done one pomodoro!
  • Finally, the second "time.sleep()" function waits 10 minutes(600 seconds) before notifying the user that his/her 10 minute break is over.

There you have it!

If you put everything together, your pomodoro timer should look something like this:

import time
from plyer import notification

count = 0
print("The pomodoro timer has started, start working!")

if __name__ == "__main__":
    while True:
        time.sleep(1800)
        count += 1
        notification.notify(
            title = "Good work!",
            message = "Take a 10 minute break! You have completed " + str(count) + " pomodoros so far",
        )
        time.sleep(600)
        notification.notify(
            title = "Back to work!",
            message = "Try doing another pomodoro...",
        )
Enter fullscreen mode Exit fullscreen mode

Get the hottest programming stuff of the week in your inbox every Friday via my newsletter!


Byeeeee👋

Discussion (2)

Collapse
matt_vey profile image
Levr

Great tutorial!

Collapse
pop1912 profile image
pavan kumar

wow, thank you very much.