吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2603|回复: 4
收起左侧

[Python 原创] 【Python】tkinter实现番茄钟

  [复制链接]
moduwusuowei 发表于 2025-6-4 14:24
本帖最后由 moduwusuowei 于 2025-6-4 14:29 编辑

好久没看论坛了,今天看到一个小伙伴分享了一个番茄钟的工具,并没有提供源码,来到这个区域的人不仅对工具感兴趣,同时保留着对源码的热爱;

一开始是真不知道什么是番茄钟,搜了一下,差不多就是倒计时用的,类似围棋对决的时候有时间限制,那是两个倒计时钟,基于这个场景用python代码实现;

整体来说这段代码难度为3颗星,就是简单逻辑梳理清楚就可以动手写代码了,太复杂的计算与逻辑并没有在这个工具上使用;

提供了源码,没有额外的第三方库,可以在本地直接运行,新手小白也能搞定;

import tkinter as tk
from tkinter import messagebox
import time

class TomatoClock:
    def __init__(self, root):
        self.root = root
        self.debug = True
        self.root.title("番茄钟_moduwusuowei")
        self.root.geometry("250x100")

        self.time_entry = tk.Entry(root, width=30, font=('Helvetica', 22))
        self.time_entry.pack(pady=5, padx=5)

        self.start_button = tk.Button(root, text="开始", command=self.start_countdown, font=('Helvetica', 22))
        self.start_button.pack(pady=5, padx=5)

        self.countdown_label = tk.Label(root, text="", font=('Helvetica', 28))
        self.countdown_label.pack(pady=25)

        self.is_running = False

    def start_countdown(self):
        if self.is_running:
            return
        self.is_running = True
        self.time_entry.pack_forget()
        self.start_button.pack_forget()
        try:
            if self.debug:
                total_seconds = int(self.time_entry.get())
            else:
                total_seconds = int(self.time_entry.get()) * 60
        except ValueError:
            messagebox.showerror("错误", "请输入有效的分钟数")
            self.reset_clock()
            return

        while total_seconds > 0:
            print(f"Remaining: {total_seconds} seconds")
            if self.debug:
                mins = 0
                secs = total_seconds
            else:
                mins, secs = divmod(total_seconds, 60)
            text = f"{mins:02d}:{secs:02d}"
            print(f"{text=}")
            self.countdown_label.pack()
            self.countdown_label.config(text=text)
            self.root.update()
            time.sleep(1)
            total_seconds -= 1
        messagebox.showinfo("时间到!", "您的番茄钟时间已经结束。")
        self.reset_clock()

    def reset_clock(self):
        self.is_running = False
        self.countdown_label.pack_forget()
        self.countdown_label.config(text="")
        self.time_entry.delete(0, tk.END)
        self.time_entry.pack(pady=5, padx=5)
        self.start_button.pack(pady=5, padx=5)

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



运行效果也比较简洁:

番茄钟

番茄钟

免费评分

参与人数 5吾爱币 +9 热心值 +5 收起 理由
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
helian147 + 2 + 1 热心回复!
freckle + 1 + 1 谢谢@Thanks!
抱薪风雪雾 + 1 + 1 谢谢@Thanks!
confiant + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

sktao 发表于 2025-6-4 14:38
这个上面显示时间,下面再有闹钟就更好了,然后闹钟增加提醒事件 可以选择每天都这样,
猪肉炖粉条 发表于 2025-6-5 06:23
yilong88888 发表于 2025-6-5 09:16
抱薪风雪雾 发表于 2025-6-5 10:06
多谢,分享好玩好用的小软
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - 52pojie.cn ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2026-11-4 07:21

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表