183 lines
5.4 KiB
Python
183 lines
5.4 KiB
Python
import time
|
|
import threading
|
|
import sys
|
|
|
|
class Timer:
|
|
def __init__(self):
|
|
self.total_seconds = 0
|
|
self.remaining_seconds = 0
|
|
self.is_running = False
|
|
self.is_paused = False
|
|
self.thread = None
|
|
|
|
def set_timer(self, minutes, seconds):
|
|
"""设置倒计时时间"""
|
|
self.total_seconds = minutes * 60 + seconds
|
|
self.remaining_seconds = self.total_seconds
|
|
print(f"计时器设置为 {minutes} 分 {seconds} 秒")
|
|
|
|
def start(self):
|
|
"""开始倒计时"""
|
|
if self.total_seconds <= 0:
|
|
print("请先设置计时器时间!")
|
|
return
|
|
|
|
if self.is_running and not self.is_paused:
|
|
print("计时器已经在运行中!")
|
|
return
|
|
|
|
self.is_running = True
|
|
self.is_paused = False
|
|
|
|
if self.thread is None or not self.thread.is_alive():
|
|
self.thread = threading.Thread(target=self._countdown)
|
|
self.thread.daemon = True
|
|
self.thread.start()
|
|
print("计时器开始!")
|
|
|
|
def pause(self):
|
|
"""暂停计时器"""
|
|
if not self.is_running:
|
|
print("计时器未运行!")
|
|
return
|
|
|
|
if self.is_paused:
|
|
print("计时器已经暂停!")
|
|
return
|
|
|
|
self.is_paused = True
|
|
print("计时器已暂停")
|
|
|
|
def resume(self):
|
|
"""继续计时器"""
|
|
if not self.is_running:
|
|
print("计时器未运行!")
|
|
return
|
|
|
|
if not self.is_paused:
|
|
print("计时器未暂停!")
|
|
return
|
|
|
|
self.is_paused = False
|
|
print("计时器继续")
|
|
|
|
def reset(self):
|
|
"""重置计时器"""
|
|
self.is_running = False
|
|
self.is_paused = False
|
|
self.remaining_seconds = self.total_seconds
|
|
print("计时器已重置")
|
|
|
|
def _countdown(self):
|
|
"""倒计时线程函数"""
|
|
while self.remaining_seconds > 0 and self.is_running:
|
|
if not self.is_paused:
|
|
mins, secs = divmod(self.remaining_seconds, 60)
|
|
time_format = f"{mins:02d}:{secs:02d}"
|
|
sys.stdout.write(f"\r剩余时间: {time_format}")
|
|
sys.stdout.flush()
|
|
time.sleep(1)
|
|
self.remaining_seconds -= 1
|
|
|
|
if self.remaining_seconds <= 0 and self.is_running:
|
|
print("\n时间到!计时器结束!")
|
|
self.is_running = False
|
|
else:
|
|
print("\n计时器已停止")
|
|
|
|
def get_status(self):
|
|
"""获取计时器状态"""
|
|
if self.is_running:
|
|
if self.is_paused:
|
|
status = "已暂停"
|
|
else:
|
|
status = "运行中"
|
|
else:
|
|
status = "未运行"
|
|
|
|
mins, secs = divmod(self.remaining_seconds, 60)
|
|
time_format = f"{mins:02d}:{secs:02d}"
|
|
return f"状态: {status}, 剩余时间: {time_format}"
|
|
|
|
|
|
def main():
|
|
"""主函数,提供命令行交互界面"""
|
|
timer = Timer()
|
|
|
|
print("=== Python 计时器 ===")
|
|
print("命令列表:")
|
|
print(" set <分钟> <秒> - 设置计时器时间")
|
|
print(" start - 开始计时器")
|
|
print(" pause - 暂停计时器")
|
|
print(" resume - 继续计时器")
|
|
print(" reset - 重置计时器")
|
|
print(" status - 查看状态")
|
|
print(" help - 显示帮助")
|
|
print(" quit - 退出程序")
|
|
print()
|
|
|
|
while True:
|
|
try:
|
|
command = input(">>> ").strip().lower().split()
|
|
|
|
if not command:
|
|
continue
|
|
|
|
cmd = command[0]
|
|
|
|
if cmd == "set":
|
|
if len(command) != 3:
|
|
print("用法: set <分钟> <秒>")
|
|
continue
|
|
try:
|
|
minutes = int(command[1])
|
|
seconds = int(command[2])
|
|
if minutes < 0 or seconds < 0:
|
|
print("时间不能为负数!")
|
|
continue
|
|
timer.set_timer(minutes, seconds)
|
|
except ValueError:
|
|
print("请输入有效的数字!")
|
|
|
|
elif cmd == "start":
|
|
timer.start()
|
|
|
|
elif cmd == "pause":
|
|
timer.pause()
|
|
|
|
elif cmd == "resume":
|
|
timer.resume()
|
|
|
|
elif cmd == "reset":
|
|
timer.reset()
|
|
|
|
elif cmd == "status":
|
|
print(timer.get_status())
|
|
|
|
elif cmd == "help":
|
|
print("命令列表:")
|
|
print(" set <分钟> <秒> - 设置计时器时间")
|
|
print(" start - 开始计时器")
|
|
print(" pause - 暂停计时器")
|
|
print(" resume - 继续计时器")
|
|
print(" reset - 重置计时器")
|
|
print(" status - 查看状态")
|
|
print(" help - 显示帮助")
|
|
print(" quit - 退出程序")
|
|
|
|
elif cmd == "quit":
|
|
print("退出计时器程序")
|
|
break
|
|
|
|
else:
|
|
print(f"未知命令: {cmd},输入 'help' 查看帮助")
|
|
|
|
except KeyboardInterrupt:
|
|
print("\n程序被中断")
|
|
break
|
|
except Exception as e:
|
|
print(f"发生错误: {e}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |