import tkinter as tk
from tkinter import ttk, messagebox
import time
import random

class TypingTestApp:
    def __init__(self, root):
        self.root = root
        self.root.title("打字练习软件")
        self.root.geometry("700x500")

        # 练习文本库，可以自己增删
        self.text_list = [
            "The quick brown fox jumps over the lazy dog.",
            "Python is a powerful and easy-to-learn programming language.",
            "Practice makes perfect when you are learning to type fast.",
            "生活原本沉闷，但跑起来就有风。",
            "编程的乐趣在于不断解决一个又一个有趣的问题。"
        ]

        self.start_time = None
        self.is_started = False
        self.current_text = ""

        # 标题
        title_label = ttk.Label(root, text="打字练习", font=("微软雅黑", 20, "bold"))
        title_label.pack(pady=10)

        # 对照文本区域
        ttk.Label(root, text="对照文本：", font=("微软雅黑",12)).pack(anchor="w", padx=20)
        self.sample_text = tk.Text(root, height=3, wrap=tk.WORD, font=("微软雅黑",13))
        self.sample_text.pack(fill=tk.X, padx=20)
        self.sample_text.config(state=tk.DISABLED)

        # 输入框
        ttk.Label(root, text="在这里输入：", font=("微软雅黑",12)).pack(anchor="w", padx=20, pady=(10,0))
        self.input_box = tk.Text(root, height=5, wrap=tk.WORD, font=("微软雅黑",13))
        self.input_box.pack(fill=tk.X, padx=20)

        # 数据显示行
        info_frame = ttk.Frame(root)
        info_frame.pack(pady=10)
        self.speed_label = ttk.Label(info_frame, text="速度：0 WPM", font=("微软雅黑",12))
        self.speed_label.grid(row=0, column=0, padx=15)
        self.acc_label = ttk.Label(info_frame, text="正确率：0 %", font=("微软雅黑",12))
        self.acc_label.grid(row=0, column=1, padx=15)

        # 按钮
        btn_frame = ttk.Frame(root)
        btn_frame.pack()
        self.start_btn = ttk.Button(btn_frame, text="开始练习", command=self.start_test)
        self.start_btn.grid(row=0, column=0, padx=10)
        self.end_btn = ttk.Button(btn_frame, text="结束统计", command=self.finish_test, state=tk.DISABLED)
        self.end_btn.grid(row=0, column=1, padx=10)

    def start_test(self):
        # 随机选取文本
        self.current_text = random.choice(self.text_list)
        self.sample_text.config(state=tk.NORMAL)
        self.sample_text.delete(1.0, tk.END)
        self.sample_text.insert(tk.END, self.current_text)
        self.sample_text.config(state=tk.DISABLED)

        self.input_box.delete(1.0, tk.END)
        self.input_box.focus()

        self.start_time = time.time()
        self.is_started = True
        self.start_btn.config(state=tk.DISABLED)
        self.end_btn.config(state=tk.NORMAL)

        self.speed_label.config(text="速度：0 WPM")
        self.acc_label.config(text="正确率：0 %")

    def finish_test(self):
        if not self.is_started:
            return
        end_time = time.time()
        used_time = end_time - self.start_time
        user_input = self.input_box.get(1.0, tk.END).strip()

        # 逐字符比对
        correct = 0
        min_len = min(len(user_input), len(self.current_text))
        for a, b in zip(user_input[:min_len], self.current_text[:min_len]):
            if a == b:
                correct += 1
        total = len(self.current_text)
        accuracy = correct / total * 100 if total > 0 else 0

        # WPM 英文打字速度（单词/分钟）
        word_count = len(self.current_text.split())
        minute = used_time / 60
        wpm = word_count / minute if minute > 0 else 0

        self.speed_label.config(text=f"速度：{wpm:.1f} WPM")
        self.acc_label.config(text=f"正确率：{accuracy:.1f} %")

        messagebox.showinfo("练习结果",
            f"用时：{used_time:.1f} 秒\n"
            f"正确字符：{correct}/{total}\n"
            f"正确率：{accuracy:.1f}%\n"
            f"打字速度：{wpm:.1f} WPM")

        self.is_started = False
        self.start_btn.config(state=tk.NORMAL)
        self.end_btn.config(state=tk.DISABLED)

if __name__ == "__main__":
    root = tk.Tk()
    app = TypingTestApp(root)
    root.mainloop()
