GUI设计一个秒表计时器
时间: 2023-10-19 19:07:22 浏览: 251
好的,以下是一个简单的秒表计时器的GUI设计,使用Python的tkinter模块实现:
```python
import tkinter as tk
class Stopwatch:
def __init__(self, master):
self.master = master
master.title("秒表计时器")
# 创建和放置组件
self.time_label = tk.Label(master, text="00:00:00", font=("Arial", 80))
self.time_label.pack()
self.start_button = tk.Button(master, text="开始", command=self.start)
self.start_button.pack(side="left", padx=10)
self.stop_button = tk.Button(master, text="停止", command=self.stop)
self.stop_button.pack(side="left", padx=10)
self.reset_button = tk.Button(master, text="重置", command=self.reset)
self.reset_button.pack(side="left", padx=10)
# 初始化变量
self.running = False
self.elapsed_time = 0
self.start_time = 0
self.update_time()
def update_time(self):
"""更新时间标签,每100毫秒调用一次"""
if self.running:
elapsed_time = int((tk.Event().time - self.start_time) / 100)
if elapsed_time != self.elapsed_time:
self.elapsed_time = elapsed_time
minutes, seconds = divmod(elapsed_time, 600)
seconds, hundredths = divmod(seconds, 10)
self.time_label.config(text=f"{minutes:02d}:{seconds:02d}:{hundredths:02d}")
self.time_label.after(100, self.update_time)
def start(self):
"""开始计时"""
self.running = True
self.start_time = tk.Event().time
def stop(self):
"""停止计时"""
self.running = False
def reset(self):
"""重置计时器"""
self.running = False
self.elapsed_time = 0
self.time_label.config(text="00:00:00")
root = tk.Tk()
stopwatch = Stopwatch(root)
root.mainloop()
```
运行这个程序,就可以看到一个简单的秒表计时器GUI界面,包括一个时间标签和三个按钮(开始、停止、重置),点击按钮即可开始、停止、重置计时器。
阅读全文