优化这段python代码import time import tkinter as tk # 定义全局变量 WORK_TIME = 25 REST_TIME = 5 LONG_REST_TIME = 15count = 0 # 创建窗口window = tk.Tk()window.title("番茄钟")window.geometry("300x200") # 创建标签label = tk.Label(text="番茄钟", font=("Arial", 20))label.pack() # 创建倒计时标签 time_label = tk.Label(text="", font=("Arial", 18)) time_label.pack() # 创建开始按钮 start_button = tk.Button(text="开始", font=("Arial", 12)) def start_timer(): global count count += 1 if count % 8 == 0: # 每四次休息一次长休息 count_down(LONG_REST_TIME * 60) label.config(text="休息时间") window.config(background="lightblue") elif count % 2 == 0: # 每两次休息一次短休息 count_down(REST_TIME * 60) label.config(text="休息时间") window.config(background="lightgreen") else: count_down(WORK_TIME * 60) label.config(text="工作时间") window.config(background="pink") def count_down(seconds): for i in range(seconds, 0, -1): minute = i // 60 second = i % 60 time_label.config(text="{:02d}:{:02d}".format(minute, second)) window.update() time.sleep(1) # 绑定按钮的事件处理函数 start_button.config(command=start_timer) start_button.pack() window.mainloop()
时间: 2024-01-03 18:02:54 浏览: 65
以下是对这段Python代码的优化:
1. 将全局变量移动到函数内部,在函数内使用非局部变量来代替全局变量,提高了代码的可维护性。
2. 将计时器的倒计时逻辑封装到一个类中,使代码更加面向对象。
3. 优化了计时器的更新方式,使用`after()`函数替代`time.sleep()`,避免了阻塞主线程的问题。
4. 为窗口和控件设置了统一的样式,提高了程序的美观度。
优化后的代码如下:
```python
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()
```
这个程序使用了面向对象的方式,将计时器的倒计时逻辑封装到`TomatoClock`类中,提高了代码的可读性和可维护性。倒计时的更新方式使用`after()`函数替代了`time.sleep()`,避免了阻塞主线程的问题。同时,为窗口和控件设置了统一的样式,提高了程序的美观度。
阅读全文