计时器(Python)

代码

import time
from tkinter import ttk
import threading
from tkinter import scrolledtext
import tkinter as tk


class TimerApp:
    def __init__(self, root):
        self.root = root
        self.root.title("计时器")
        self.screen_w, self.screen_h = self.root.winfo_screenwidth(), self.root.winfo_screenheight()
        self.w, self.h = int(self.screen_w / 3), int(self.screen_h / 3)
        self.root.geometry(f'{self.w}x{self.h}+{int(self.screen_w / 4)}+{int(self.screen_h / 4)}')
        self.root.resizable(False, False)

        self.running = False
        self.paused = False
        self.start_time = None
        self.pause_time = None
        self.count = 0

        self.time_label = ttk.Label(
            self.root, text="0.0000", font=("微软雅黑", 32), foreground="#FFFFFF", background="#c9d8ee")
        self.time_label.place(relx=.4, rely=.2)
        self.box = scrolledtext.ScrolledText(self.root, width=12, height=10)
        self.box.place(relx=.1, rely=.3)

        ttk.Button(self.root, text="Start", command=self.start_timer, width=8).place(relx=.4, rely=.5)
        ttk.Button(self.root, text="Pause", command=self.pause_timer, width=8).place(relx=.4, rely=.6)
        ttk.Button(self.root, text="Continue", command=self.continue_timer, width=8).place(relx=.55, rely=.6)
        ttk.Button(self.root, text="Stop", command=self.stop_timer, width=8).place(relx=.55, rely=.5)
        ttk.Button(self.root, text="Clear", command=self.clear_box, width=5).place(relx=.18, rely=.79)
        ttk.Button(self.root, text="Get", command=self.insert_timer, width=8).place(relx=.4, rely=.7)
        ttk.Button(self.root, text="Restore", command=self.restore_time_label, width=8
                   ).place(relx=.55, rely=.7)
        ttk.Button(self.root, text="Close", command=lambda: self.root.destroy(), width=8).place(relx=.85, rely=.9)

    def restore_time_label(self):
        self.stop_timer()
        self.time_label.config(text="0.0000")

    def clear_box(self):
        self.count = 0
        self.box.delete("1.0", "end")

    def start_timer(self):
        if not self.running:
            self.running = True
            self.start_time = time.perf_counter()
            self.time_thread = threading.Thread(target=self.update_time)
            self.time_thread.start()

    def pause_timer(self):
        if self.running and not self.paused:
            self.paused = True
            self.pause_time = time.perf_counter()
            # self.count += 1
            # self.box.insert("end", "<{0}>{1:.4f}\n".format(self.count, self.main_time))

    def insert_timer(self):
        if self.main_time and self.running:
            self.count += 1
            self.box.insert("end", "<{0}>{1:.4f}\n".format(self.count, self.main_time))

    def continue_timer(self):
        if self.paused:
            self.paused = False
            self.start_time += time.perf_counter() - self.pause_time

    def stop_timer(self):
        self.running = False
        self.paused = False
        try:
            if self.time_thread is not None:
                self.time_thread.join()
                # self.time_label.config(text="0.0000")
        except AttributeError:
            self.root.destroy()

    def update_time(self):
        while self.running:
            if not self.paused:
                self.main_time = time.perf_counter() - self.start_time
                self.time_label.config(text="{:.4f}".format(self.main_time))
            time.sleep(0.1)

    def on_closing(self):
        self.stop_timer()
        self.root.destroy()


if __name__ == "__main__":
    root = tk.Tk()
    app = TimerApp(root)
    root.protocol("WM_DELETE_WINDOW", app.on_closing)
    root.mainloop()

 效果

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值