pomodoro gui

Pomodoro GUI App

Python Tkinter Project

Since the 3-part Python Tkinter Tutorial Series has come to an end, let us build a Project based on it. In this concluding article for Tkinter, we shall create a Pomodoro GUI App.

What is Pomodoro?

Pomodoro is a famous Time Management Technique that helps us focus on Work and as wells as breaks. In Pomodoro, one must first focus on Work for 25 minutes straight and then take a 5 minutes break to relax. This technique is valued and practiced a lot in day-to-day life.

Working this way allows your brain to pick up so you can steer into your successive work session with a clear state of mind. It is recommended to start with 25 minutes of work and a 5-minute break to get into a comfortable zone with this technique. However, you can also set time intervals for the workflow part and then take a longer break.

In order to know more about Pomodoro: Check out this Wikipedia Article.

How To Create Pomodoro GUI Using Tkinter?

First Import the necessary modules.

The time module is basically used to add a one-second delay i.e., time.sleep(1) The Pygame module is used to play a sound once work or break time is completed.

pygame.mixer.init()
pygame.mixer.music.load("baka.ogg")
pygame.mixer.music.play(1) #1 means play onces

Tkinter message box is used to provide a feedback message alongside the Pygame music. So that the user gets an update to take a break or go back to work.

import tkinter as tk
import pygame
import time
import tkinter.messagebox as msg

GUI Root Window

This is a basic GUI window configuration code. We had a red background with help of the Canvas Tkinter Widget. Next, we add a Label displaying the countdown clock. Last but not least we created two buttons one for work and another one for a break.

Note: Download the sound.ogg used in the file.

pygame.mixer.init()
pygame.mixer.music.load("sound.ogg") 

root = tk.Tk()
root.geometry("300x300+470+180")
root.resizable(False, False)
root.title("Pomodoro")

canvas = tk.Canvas(bg="red")
canvas.place(x=-1, y=-1, width=330, height=330)

label_time = tk.Label(text="25:00",font=("arial",80,"bold"))
label_time.pack(fill="y",pady=20,padx=6)

work = tk.Button(text="Work", command=work_clock,bg="blue",fg="white",bd=4,font=("arial",18,"bold")).place(x=50, y=220)
take_a_break = tk.Button(text="Break", command=break_clock,bg="blue",fg="white",bd=4,font=("arial",18,"bold")).place(x=160, y=220)

root.mainloop()

Since we have added two buttons, it makes sense to add two trigger functions using the command argument inside the button widget. Since the timing workflow is the same for both functions we create a new function to avoid the repetition of code.

def main_clock(total_minutes,heading,message):
    total_secs = int(total_minutes * 60)
    while True:
        sec = total_secs%60
        min = total_secs//60
        #min, sec = divmod(total_secs,60)
        time.sleep(1)
        label_time.config(text=f"{min}:{sec}")
        total_secs = total_secs-1
        if total_secs < 0:
            pygame.mixer.music.play(1)
            msg.showinfo(heading, message)
            break
        root.update() 

In the main clock function, we multiply the total minutes by 60 to get the total seconds. 25 minutes is the total time for work and 5 minutes for a break. In order to get minute and second after each second, we use time.sleep(1) and root.update() method. root.update() method is used whenever there is an event change in GUI with respect to time. For the label to change after each second we have a formula.

def work_clock():
    main_clock(25,"Take a Break", "Click the break button to start the break timer")

def break_clock():
    main_clock(5,"Go back to work", "Click the work button to start the work timer")

We pass three arguments inside main_clock one is total minute, and the next two are heading and messages for Tkinter Messagebox.

Python Divmod Function

To get the minute we use total_seconds//60 and to get the seconds we use the reminder i.e., total_seconds%60. We have a built function in Python that is used for this task. Divmod is a built-in Python function that takes in two arguments and returns a Tuple. The tuple is in form of (x//y,x%y) where x and y are arguments passed in Divmod.

Syntax:
x_div_y,x_mod_y = divmod(x,y) 
sec = total_secs%60
min = total_secs//60

or

min, sec = divmod(total_secs,60)

For example, let us check the overall time of 1210 seconds in terms of both minutes and seconds.

>>> min,sec = divmod(1210,60)
>>> min
20
>>> sec
10

Putting All Together: Pomodoro GUI

import tkinter as tk
import pygame
import time
import tkinter.messagebox as msg

def main_clock(total_minutes,heading,message):
    total_secs = int(total_minutes * 60)
    while True:
        sec = total_secs%60
        min = total_secs//60
        #min, sec = divmod(total_secs,60)
        time.sleep(1)
        label_time.config(text=f"{min}:{sec}")
        total_secs = total_secs-1
        if total_secs < 0:
            pygame.mixer.music.play(1)
            msg.showinfo(heading, message)
            break
        root.update()

def work_clock():
    main_clock(25,"Take a Break", "Click the break button to start the break timer")

def break_clock():
    main_clock(5,"Go back to work", "Click the work button to start the work timer")

try:
    pygame.mixer.init()
    pygame.mixer.music.load("sound.ogg")
except pygame.error:
    print("Download sound.ogg or add any .ogg music file")

root = tk.Tk()
root.geometry("300x300+470+180")
root.resizable(False, False)
root.title("Pomodoro")

canvas = tk.Canvas(bg="red")
canvas.place(x=-1, y=-1, width=330, height=330)

label_time = tk.Label(text="25:00",font=("arial",80,"bold"))
label_time.pack(fill="y",pady=20,padx=6)

work = tk.Button(text="Work", command=work_clock,bg="blue",fg="white",bd=4,font=("arial",18,"bold")).place(x=50, y=220)
take_a_break = tk.Button(text="Break", command=break_clock,bg="blue",fg="white",bd=4,font=("arial",18,"bold")).place(x=160, y=220)

root.mainloop()

Subscribe to our Newsletter to never miss out on the content: https://animevyuh.org/newsletter. Join our Newsletter now for amazing Anime recommendationsPython, and Machine Learning Content.

Support Us: https://www.buymeacoffee.com/trjtarunhttps://ko-fi.com/tarunrjain751.
GitHub: https://github.com/lucifertrj.
Twitter: https://twitter.com/TRJ_0751.

Want to learn Machine Learning with Proper Roadmap and resource check this Repository: https://github.com/lucifertrj/100DaysOfML

1 thought on “Pomodoro GUI App”

  1. Pingback: Tkinter MessageBox And FileDialog

Comments are closed.