📜  45 分钟计时器 (1)

📅  最后修改于: 2023-12-03 15:13:08.680000             🧑  作者: Mango

45分钟计时器

简介

这是一款基于Python语言开发的45分钟计时器。使用Tkinter图形界面库构建用户界面,实现简单易用的计时功能。

功能特点
  • 计时器默认设置为45分钟,用户可通过+/-按钮进行时间修改。
  • 计时器支持开始、暂停、重置计时功能。
  • 计时结束时,程序会自动弹出提示框进行提醒。
运行环境要求
  • Python 3.x
  • Tkinter图形界面库
安装与使用
  1. 将代码clone到本地目录

    git clone https://github.com/xxx/45min-timer.git
    
  2. 进入目录

    cd 45min-timer
    
  3. 运行程序

    python timer.py
    
代码片段
from tkinter import *
import time

class Timer:
    def __init__(self, master):
        self.master = master
        master.title("45分钟计时器")

        self.time_var = IntVar()
        self.time_var.set(45)

        self.time_label = Label(master, textvariable=self.time_var, font=("Helvetica", 48))
        self.time_label.pack()

        self.start_button = Button(master, text="开始", command=self.start_timer)
        self.start_button.pack(side=LEFT, padx=5)

        self.pause_button = Button(master, text="暂停", command=self.pause_timer)
        self.pause_button.pack(side=LEFT, padx=5)

        self.reset_button = Button(master, text="重置", command=self.reset_timer)
        self.reset_button.pack(side=LEFT, padx=5)

        self.inc_button = Button(master, text="+", command=self.inc_timer)
        self.inc_button.pack(side=LEFT, padx=5)

        self.dec_button = Button(master, text="-", command=self.dec_timer)
        self.dec_button.pack(side=LEFT, padx=5)

        self.time_running = False
        self.time_paused = False
        self.start_time = None

    def update_time(self):
        if self.time_running:
            elapsed_time = time.time() - self.start_time
            remaining_time = self.time_var.get() * 60 - elapsed_time
            if remaining_time <= 0:
                self.time_var.set(0)
                self.time_running = False
                self.time_paused = False
                self.start_time = None
                self.show_alert()
            else:
                minutes = int(remaining_time // 60)
                seconds = int(remaining_time % 60)
                self.time_var.set(minutes)
                self.master.after(1000, self.update_time)

    def start_timer(self):
        if not self.time_running and not self.time_paused:
            self.time_running = True
            self.start_time = time.time()
            self.update_time()

        elif self.time_paused:
            self.time_paused = False
            self.start_time = time.time() - self.elapsed_time
            self.update_time()

    def pause_timer(self):
        if self.time_running and not self.time_paused:
            self.time_paused = True
            self.elapsed_time = time.time() - self.start_time
            self.update_time()

    def reset_timer(self):
        self.time_var.set(45)
        self.time_running = False
        self.time_paused = False
        self.start_time = None

    def inc_timer(self):
        current_time = self.time_var.get()
        if current_time < 60:
            self.time_var.set(current_time + 1)

    def dec_timer(self):
        current_time = self.time_var.get()
        if current_time > 1:
            self.time_var.set(current_time - 1)

    def show_alert(self):
        messagebox.showinfo("时间到", "45分钟计时结束!")

root = Tk()
timer = Timer(root)
root.mainloop()

以上是45分钟计时器的代码片段。这段代码实现了计时器的各种功能,包括开始、暂停、重置计时、以及计时结束时的提醒。同时,我们也实现了可视化的UI界面。这个计时器可以广泛应用于个人工作和学习中,为用户提供更好的时间管理体验。