Create a Pomodoro Using Python Tkinter


What is Pomodoro?

Francesco Cirillo, a university student at the time, invented the Pomodoro Method in the late 1980s. Cirillo was having difficulty focusing on his academics and completing homework. He asked himself, feeling overwhelmed, to commit to only 10 minutes of dedicated study time. Inspired by the challenge, he discovered a tomato-shaped kitchen timer (pomodoro in Italian), and the Pomodoro technique was created.In this tutorial, we will be creating a Pomodoro Timer using Python and Tkinter. The timer will be created using a GUI, and we will be using the Tkinter module to create the GUI.

Steps and Processes

Step 1 − Installing Required Libraries

Before we begin coding, we need to make sure that we have all the required libraries installed. In this tutorial, we will be using the time and tkinter modules. These modules come pre-installed with Python, so we don't need to install them separately.

Step 2 − Creating the GUI

Now that we have the required libraries, we can start creating our GUI. The first thing we need to do is create a new Python file and import the Tkinter module.

import tkinter as tk
import time
from datetime import datetime, timedelta

Next, we need to create a new instance of the Tkinter class and set the window title and dimensions.

# Create a GUI window
root = tk.Tk()
root.title("Pomodoro Timer")

# Set the window size and position
window_width = 300
window_height = 150
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x_cordinate = int((screen_width / 2) - (window_width / 2))
y_cordinate = int((screen_height / 2) - (window_height / 2))
root.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))

This code creates a new window with the title "Pomodoro Timer" and dimensions of 300 pixels by 150 pixels.

Step 3 − Adding Labels and Buttons

We now need to add labels and buttons to the GUI window that we have created. We'll include a label for the current time, a label for the countdown timer, and two buttons to start and stop the timer.

# Current Time Label
current_time_label = tk.Label(text="", font=("Helvetica", 20))
current_time_label.pack()

# Timer Label
timer_label = tk.Label(text="", font=("Helvetica", 30))
timer_label.pack()

# Start Button
start_button = tk.Button(text="Start", font=("Helvetica", 10))
start_button.pack(side="left")

# Stop Button
stop_button = tk.Button(text="Stop", font=("Helvetica", 10))
stop_button.pack(side="right")

This code generates the labels and buttons and places them in the graphical user interface window. For the time being, the current time label and timer label are empty; we will fill them with the current time and timer values later.

Step 4 − Adding Functionality to Buttons

We need to add functionality to the start and the stop buttons now that we have the labels and buttons in place. When you press the start button, the timer should begin ticking down and the current time label should reflect the current time. When you press the stop button, the timer should stop ticking down.

# Start Button Functionality
def start_timer():
   current_time_label.config(text=time.strftime("%H:%M:%S"))
   timer_label.config(text="25:00")
   start_button.config(state="disabled")
   stop_button.config(state="normal")

# Stop Button Functionality
def stop_timer():
   timer_label.config(text="")
   start_button.config(state="normal")
   stop_button.config(state="disabled")

The start timer method uses the time module's str f time function to update the current time label with the current time. It also changes the timer label to 25 minutes (the length of a Pomodoro interval) and turns off the start and stop buttons. The stop timer function clears the timer label, activates the start button, and disables the stop button.

Step 5 − Adding Timer Functionality

Now that we have the start and stop button functionality in place, we need to add the timer functionality. We will use the time module's sleep function to pause the program for one second between each countdown. We will also use the datetime module's timedelta function to calculate the remaining time and update the timer label.

# Timer Functionality
def countdown(minutes):
   seconds = minutes * 60
   while seconds > 0:
      minutes, sec = divmod(seconds, 60)
      timer_label.config(text='{:02d}:{:02d}'.format(minutes, sec))
      time.sleep(1)
      seconds -= 1
   timer_label.config(text="Time's up!")
   start_button.config(state="normal")
   stop_button.config(state="disabled")

The inputted number of minutes is converted to a corresponding number of seconds via the countdown function. If there are still seconds available, it will enter a while loop that will keep on forever. Throughout the duration of the while loop, the divmod function is used to determine how many minutes and seconds are left, and the format function is used to display that information on the timer label. Each time the countdown is performed, the software is put to sleep for a full second. As the countdown approaches zero, the timer's label changes to read "Time's up!" and the start button becomes active while the stop button becomes inactive.

Step 6 − Connecting Buttons to Functions

The final step is to connect the start and stop buttons to their respective functions. We can do this using the button's command parameter.

start_button.config(command=lambda: (start_timer(), countdown(25)))
stop_button.config(command=stop_timer)

Example

We can now run the program and test the Pomodoro timer. To run the program, save the code in a file with a .py extension and run it using the Python interpreter.

# importing the essential libraries for our work
import tkinter as tk
import time
from datetime import datetime, timedelta

# Create a GUI window
root = tk.Tk()
root.title("Pomodoro Timer")
# Set the window size and position
window_width = 300
window_height = 150
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x_cordinate = int((screen_width / 2) - (window_width / 2))
y_cordinate = int((screen_height / 2) - (window_height / 2))
root.geometry("{}x{}+{}+{}".format(window_width,
   window_height, x_cordinate, y_cordinate))
# Current Time Label
current_time_label = tk.Label(text="", font=("Helvetica", 20))
current_time_label.pack()

# Timer Label
timer_label = tk.Label(text="", font=("Helvetica", 30))
timer_label.pack()

# Start Button
start_button = tk.Button(text="Start", font=("Helvetica", 10))
start_button.pack(side="left")

# Stop Button
stop_button = tk.Button(text="Stop", font=("Helvetica", 10))
stop_button.pack(side="right")

# This code snippet is used to understand the start Button Functionality
def start_timer():
   current_time_label.config(text=time.strftime("%H:%M:%S"))
   timer_label.config(text="25:00")
   start_button.config(state="disabled")
   stop_button.config(state="normal")

# This code snippet is used to understand the stop Button Functionality
def stop_timer():
   timer_label.config(text="")
   start_button.config(state="normal")
   stop_button.config(state="disabled")

# This code snippet is used to understand the timer Functionality
def countdown(minutes):
   seconds = minutes * 60
   while seconds > 0:
      minutes, sec = divmod(seconds, 60)
      timer_label.config(text='{:02d}:{:02d}'.format(minutes, sec))
      time.sleep(1)
      seconds -= 1
   timer_label.config(text="Time's up!")
   start_button.config(state="normal")
   stop_button.config(state="disabled")

# this piece of code is used to connect the start and stop button to make it easier
start_button.config(command=lambda: (start_timer(), countdown(25)))
stop_button.config(command=stop_timer)

Output

In the above box we can see that the output of the pomodoro app with GUI can be seen and this can be used to create one.

Conclusion

Following this instruction, you will be able to use Python and Tkinter to create your own Pomodoro timer. We created a graphical user interface (GUI) window by using Tkinter module and populated it with text and control elements. To develop a countdown timer that displays the remaining time on the timer's label, we made use of the time and datetime modules and configured the start and stop buttons to function. At last, we linked the buttons to their intended actions and ran the code through its paces. If you want to get more done in less time, then this Pomodoro timer is for you.

Updated on: 21-Dec-2023

184 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements