python 写一个工作 简单 番茄钟

发布于:2025-05-11 ⋅ 阅读:(25) ⋅ 点赞:(0)

1、图

 2、需求

番茄钟(Pomodoro Technique)是一种时间管理方法,由弗朗西斯科·西里洛(Francesco Cirillo)在 20 世纪 80 年代创立。“Pomodoro”在意大利语中意为“番茄”,这个名字来源于西里洛最初使用的一个形似番茄的厨房定时器。

该方法的基本理念是将工作时间划分为固定长度的“工作时段”和“休息时段”。通常,一个完整的番茄钟周期包括 25 分钟的工作时间和 5 分钟的休息时间。每完成四个番茄钟周期后,可以进行一次较长时间的休息,比如 15 - 30 分钟。

使用番茄钟的步骤如下:

1. 选择一个待完成的任务。
2. 将番茄钟设定为 25 分钟,开始专注工作。
3. 直到番茄钟响起,在纸上画一个 X 进行记录。
4. 休息 5 分钟。
5. 每完成 4 个番茄钟,进行一次 15 - 30 分钟的长时间休息。
这种时间管理方法有助于提高工作效率,减少拖延,同时也能让大脑得到适当的休息,避免过度疲劳。

 3、代码:

import tkinter as tk
import time
import winsound

# 定义番茄钟的时间(单位:分钟)
WORK_TIME = 25
BREAK_TIME = 15
LONG_BREAK_TIME = 20  # 长时间休息时间,可在 15 - 30 分钟间调整
POMODORO_COUNT_FOR_LONG_BREAK = 4

class PomodoroScreensaver:
    def __init__(self, root):
        self.root = root
        self.root.attributes('-fullscreen', True)
        self.root.configure(bg='black')
        self.root.bind('<Escape>', self.quit_screensaver)

        self.time_left = WORK_TIME * 60
        self.is_working = True
        self.pomodoro_count = 0

        self.label = tk.Label(root, text=self.format_time(self.time_left), font=('Helvetica', 100), fg='white', bg='black')
        self.label.pack(expand=True)

        self.update_clock()

    def format_time(self, seconds):
        minutes = seconds // 60
        remaining_seconds = seconds % 60
        return f'{minutes:02d}:{remaining_seconds:02d}'

    def update_clock(self):
        if self.time_left > 0:
            self.time_left -= 1
            self.label.config(text=self.format_time(self.time_left))
            # 根据工作状态更新字体颜色
            if self.is_working:
                self.label.config(fg='red')
            else:
                self.label.config(fg='green')
            self.root.after(1000, self.update_clock)
        else:
            if self.is_working:
                self.pomodoro_count += 1
                winsound.Beep(2000, 1000)
                if self.pomodoro_count % POMODORO_COUNT_FOR_LONG_BREAK == 0:
                    self.time_left = LONG_BREAK_TIME * 60
                else:
                    self.time_left = BREAK_TIME * 60
                self.is_working = False
            else:
                winsound.Beep(2000, 1000)
                self.time_left = WORK_TIME * 60
                self.is_working = True
            # 切换状态后更新字体颜色
            if self.is_working:
                self.label.config(fg='red')
            else:
                self.label.config(fg='green')
            self.update_clock()

    def quit_screensaver(self, event=None):
        self.root.destroy()

if __name__ == "__main__":
    root = tk.Tk()
    app = PomodoroScreensaver(root)
    root.mainloop()

注:字体变色 工作红色  休息 绿色  过度有响声


网站公告

今日签到

点亮在社区的每一天
去签到