Last active
March 20, 2024 04:58
-
-
Save evdokimovm/e238b6b9012abb2b0bf54c3ee90eee32 to your computer and use it in GitHub Desktop.
pomodoro timer in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import winsound | |
import tkinter as tk | |
from PIL import Image, ImageDraw, ImageTk | |
class PomodoroClock: | |
def __init__(self, master): | |
self.master = master | |
self.total_time_in_seconds = 0 | |
self.elapsed_time_in_seconds = 0 | |
self.timer_running = False | |
self.timer_id = None | |
self.canvas = tk.Canvas() | |
self.time_string = tk.StringVar() | |
self.time_string.trace_add(["write"], self.update_initial_time) | |
self.time_entry = tk.Entry(master, textvariable=self.time_string) | |
self.time_entry.pack() | |
self.time_entry.insert(0, str(self.format_time(self.total_time_in_seconds))) | |
self.canvas = tk.Canvas(master, width=200, height=200) | |
self.canvas.pack() | |
self.start_button = tk.Button(master, text="Start", command=self.start_timer) | |
self.start_button.pack() | |
self.stop_button = tk.Button(master, text="Stop", command=self.stop_timer) | |
self.stop_button.pack() | |
self.draw_progress_bar(0) | |
def update_initial_time(self, *args): | |
try: | |
self.elapsed_time_in_seconds = 0 | |
self.total_time_in_seconds = self.convert_time_to_seconds( | |
self.time_string.get() | |
) | |
self.stop_timer() | |
self.draw_progress_bar(0) | |
except ValueError: | |
pass | |
def start_timer(self): | |
if not self.timer_running: | |
self.total_time_in_seconds = self.convert_time_to_seconds( | |
self.time_string.get() | |
) | |
self.timer_running = True | |
self.timer_id = self.master.after(1000, self.update_timer) | |
def stop_timer(self): | |
if self.timer_running: | |
self.master.after_cancel(self.timer_id) | |
self.timer_running = False | |
def update_timer(self): | |
if ( | |
self.timer_running | |
and self.elapsed_time_in_seconds < self.total_time_in_seconds | |
): | |
self.elapsed_time_in_seconds += 1 | |
self.draw_progress_bar( | |
self.elapsed_time_in_seconds / self.total_time_in_seconds | |
) | |
self.timer_id = self.master.after(1000, self.update_timer) | |
elif self.elapsed_time_in_seconds >= self.total_time_in_seconds: | |
winsound.Beep(1000, 1000) | |
self.elapsed_time_in_seconds = 0 | |
self.timer_running = False | |
def draw_progress_bar(self, progress): | |
self.canvas.delete("all") | |
image = Image.new("RGB", (200, 200), "white") | |
draw = ImageDraw.Draw(image) | |
draw.arc((10, 10, 190, 190), 0, 360, fill="grey", width=10) | |
start_angle = -90 | |
end_angle = start_angle + 360 * progress | |
draw.arc((10, 10, 190, 190), start_angle, end_angle, fill="blue", width=10) | |
draw.text( | |
(90, 90), | |
self.format_time(self.total_time_in_seconds - self.elapsed_time_in_seconds), | |
fill="black", | |
) | |
self.photo_image = ImageTk.PhotoImage(image) | |
self.canvas.create_image(0, 0, image=self.photo_image, anchor="nw") | |
def convert_time_to_seconds(self, time_string): | |
minutes, seconds = map(int, time_string.split(":")) | |
return minutes * 60 + seconds | |
def format_time(self, seconds): | |
minutes = seconds // 60 | |
remaining_seconds = seconds % 60 | |
return f"{minutes:02}:{remaining_seconds:02}" | |
root = tk.Tk() | |
clock = PomodoroClock(root) | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment