解决这段代码中工作时间后不会自动切换休息倒计时的问题import tkinter as tk class TomatoClock: def init(self, work_time=25, rest_time=5, long_rest_time=15): self.work_time = work_time * 60 self.rest_time = rest_time * 60 self.long_rest_time = long_rest_time * 60 self.count = 0 self.is_working = False self.window = tk.Tk() self.window.title("番茄钟") self.window.geometry("300x200") self.window.config(background='white') self.window.option_add("*Font", ("Arial", 20)) self.label = tk.Label(self.window, text="番茄钟", background='white') self.label.pack(pady=10) self.time_label = tk.Label(self.window, text="", background='white') self.time_label.pack(pady=20) self.start_button = tk.Button(self.window, text="开始", command=self.start_timer, background='white') self.start_button.pack(pady=10) def start_timer(self): self.is_working = not self.is_working if self.is_working: self.count += 1 if self.count % 8 == 0: self.count_down(self.long_rest_time) self.label.config(text="休息时间", foreground='white', background='lightblue') elif self.count % 2 == 0: self.count_down(self.rest_time) self.label.config(text="休息时间", foreground='white', background='lightgreen') else: self.count_down(self.work_time) self.label.config(text="工作时间", foreground='white', background='pink') else: self.label.config(text="番茄钟", foreground='black', background='white') def count_down(self, seconds): if seconds == self.work_time: self.window.config(background='pink') else: self.window.config(background='lightgreen' if seconds == self.rest_time else 'lightblue') if seconds == self.long_rest_time: self.count = 0 minute = seconds // 60 second = seconds % 60 self.time_label.config(text="{:02d}:{:02d}".format(minute, second)) if seconds > 0: self.window.after(1000, self.count_down, seconds - 1) else: self.start_timer() def run(self): self.window.mainloop() if name == 'main': clock = TomatoClock() clock.run()
时间: 2024-02-21 21:56:34 浏览: 21
在 `count_down` 函数中,当倒计时结束后应该判断当前状态,如果是工作状态,就进入休息时间倒计时;如果是休息状态,就进入工作时间倒计时。可以对 `count_down` 函数做出以下修改:
```
def count_down(self, seconds):
if seconds == self.work_time:
self.window.config(background='pink')
else:
self.window.config(background='lightgreen' if seconds == self.rest_time else 'lightblue')
if seconds == self.long_rest_time:
self.count = 0
minute = seconds // 60
second = seconds % 60
self.time_label.config(text="{:02d}:{:02d}".format(minute, second))
if seconds > 0:
self.window.after(1000, self.count_down, seconds - 1)
else:
self.is_working = not self.is_working
self.start_timer()
```
这样,当倒计时结束后,会自动切换状态并进入下一个阶段的倒计时。