编辑代码

# coding:utf-8
#JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
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()