import tkinter as tk
import random
from tkinter import messagebox

class MathPractice:
    def __init__(self, root):
        self.root = root
        self.root.title("计算小能手 - 数学练习器")
        self.root.geometry("520x380")

        # 答题统计
        self.total = 0
        self.correct = 0
        self.mode = "四则"  # 模式：四则 / 乘法表
        self.num1 = 0
        self.num2 = 0
        self.answer = 0

        # 顶部模式选择
        frame_top = tk.Frame(root)
        frame_top.pack(pady=8)
        tk.Button(frame_top, text="四则运算", width=12, command=lambda: self.change_mode("四则")).grid(row=0, column=0, padx=5)
        tk.Button(frame_top, text="九九乘法表", width=12, command=lambda: self.change_mode("乘法表")).grid(row=0, column=1, padx=5)
        tk.Button(frame_top, text="重置统计", width=12, command=self.reset_stat).grid(row=0, column=2, padx=5)

        # 题目显示区域
        self.question_label = tk.Label(root, text="准备开始", font=("SimHei", 24))
        self.question_label.pack(pady=30)

        # 输入框
        frame_input = tk.Frame(root)
        frame_input.pack()
        tk.Label(frame_input, text="你的答案：", font=("SimHei", 14)).grid(row=0, column=0)
        self.ans_entry = tk.Entry(frame_input, font=("SimHei", 16), width=10)
        self.ans_entry.grid(row=0, column=1, padx=10)
        self.ans_entry.bind("<Return>", self.check_answer)  # 回车提交

        # 按钮区域
        frame_btn = tk.Frame(root)
        frame_btn.pack(pady=15)
        tk.Button(frame_btn, text="提交答案", command=self.check_answer, width=10, font=("SimHei",12)).grid(row=0, column=0, padx=6)
        tk.Button(frame_btn, text="下一题", command=self.next_question, width=10, font=("SimHei",12)).grid(row=0, column=1, padx=6)

        # 结果提示
        self.result_label = tk.Label(root, text="", font=("SimHei",14))
        self.result_label.pack(pady=8)

        # 统计信息
        self.stat_label = tk.Label(root, text="答题：0题 | 正确：0题 | 正确率：0%", font=("SimHei",12))
        self.stat_label.pack(pady=5)

        self.next_question()

    def change_mode(self, m):
        self.mode = m
        self.next_question()

    def reset_stat(self):
        self.total = 0
        self.correct = 0
        self.update_stat()
        self.result_label.config(text="统计已清空")

    def update_stat(self):
        if self.total == 0:
            rate = 0
        else:
            rate = round(self.correct / self.total * 100, 1)
        self.stat_label.config(text=f"答题：{self.total}题 | 正确：{self.correct}题 | 正确率：{rate}%")

    def next_question(self):
        self.result_label.config(text="")
        self.ans_entry.delete(0, tk.END)
        if self.mode == "乘法表":
            # 九九乘法 1~9
            self.num1 = random.randint(1,9)
            self.num2 = random.randint(1,9)
            self.answer = self.num1 * self.num2
            self.question_label.config(text=f"{self.num1} × {self.num2} = ?")
        else:
            # 四则运算，数字范围1~50，除法保证整除
            self.num1 = random.randint(1,50)
            self.num2 = random.randint(1,50)
            op = random.choice(["+","-","*","/"])
            if op == "+":
                self.answer = self.num1 + self.num2
            elif op == "-":
                # 避免负数
                if self.num1 < self.num2:
                    self.num1, self.num2 = self.num2, self.num1
                self.answer = self.num1 - self.num2
            elif op == "*":
                self.answer = self.num1 * self.num2
            else:
                # 除法：保证整除
                self.num2 = random.randint(1,12)
                self.answer = random.randint(1,20)
                self.num1 = self.num2 * self.answer
            self.question_label.config(text=f"{self.num1} {op} {self.num2} = ?")
        self.ans_entry.focus()

    def check_answer(self, event=None):
        content = self.ans_entry.get().strip()
        if not content.isdigit():
            messagebox.showwarning("提示", "请输入数字！")
            return
        user_ans = int(content)
        self.total += 1
        if user_ans == self.answer:
            self.correct += 1
            self.result_label.config(text="✅ 回答正确！", fg="green")
        else:
            self.result_label.config(text=f"❌ 答错啦，正确答案：{self.answer}", fg="red")
        self.update_stat()

if __name__ == "__main__":
    win = tk.Tk()
    app = MathPractice(win)
    win.mainloop()