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

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

        # 题目数据
        self.question_text = tk.StringVar()
        self.answer_input = tk.StringVar()
        self.score = 0
        self.total = 0
        self.current_ans = 0
        self.mode = tk.StringVar(value="四则运算")  # 模式选择

        self.create_widgets()
        self.next_question()

    def create_widgets(self):
        # 模式选择区域
        frame_mode = ttk.Frame(self.root, padding=10)
        frame_mode.pack(fill="x")
        ttk.Label(frame_mode, text="练习模式：").pack(side="left")
        ttk.Radiobutton(frame_mode, text="四则运算", variable=self.mode,
                        value="四则运算", command=self.next_question).pack(side="left", padx=8)
        ttk.Radiobutton(frame_mode, text="九九乘法表", variable=self.mode,
                        value="九九乘法表", command=self.next_question).pack(side="left", padx=8)

        # 题目显示
        frame_q = ttk.Frame(self.root, padding=20)
        frame_q.pack()
        ttk.Label(frame_q, textvariable=self.question_text, font=("微软雅黑", 24)).pack()

        # 答题输入
        frame_input = ttk.Frame(self.root, padding=10)
        frame_input.pack()
        ttk.Label(frame_input, text="你的答案：", font=("微软雅黑",14)).pack(side="left")
        entry = ttk.Entry(frame_input, textvariable=self.answer_input,
                          font=("微软雅黑",16), width=10)
        entry.pack(side="left", padx=10)
        entry.bind("<Return>", self.check_answer)  # 回车直接提交

        # 按钮区
        frame_btn = ttk.Frame(self.root, padding=10)
        frame_btn.pack()
        ttk.Button(frame_btn, text="提交答案", command=self.check_answer).pack(side="left", padx=5)
        ttk.Button(frame_btn, text="下一题", command=self.next_question).pack(side="left", padx=5)
        ttk.Button(frame_btn, text="重置分数", command=self.reset_score).pack(side="left", padx=5)

        # 分数显示
        self.label_score = ttk.Label(self.root, text=f"得分：{self.score} / 总答题：{self.total}",
                                     font=("微软雅黑",13))
        self.label_score.pack(pady=15)

    # 生成四则运算题目
    def make_four_calc(self):
        op_list = ["+", "-", "*", "/"]
        op = random.choice(op_list)
        a = random.randint(1, 30)
        b = random.randint(1, 30)
        if op == "+":
            res = a + b
        elif op == "-":
            # 不出现负数
            if a < b:
                a, b = b, a
            res = a - b
        elif op == "*":
            res = a * b
        else:
            # 除法保证整除
            b = random.randint(1,12)
            res = random.randint(1,12)
            a = b * res
        return f"{a} {op} {b} = ?", res

    # 生成九九乘法题目
    def make_multi_table(self):
        a = random.randint(1,9)
        b = random.randint(1,9)
        res = a * b
        return f"{a} × {b} = ?", res

    # 切换下一题
    def next_question(self):
        self.answer_input.set("")
        if self.mode.get() == "四则运算":
            q, ans = self.make_four_calc()
        else:
            q, ans = self.make_multi_table()
        self.question_text.set(q)
        self.current_ans = ans

    # 核对答案
    def check_answer(self, event=None):
        user_str = self.answer_input.get().strip()
        if not user_str.isdigit():
            messagebox.showwarning("提示", "请输入数字答案！")
            return
        user_ans = int(user_str)
        self.total += 1
        if user_ans == self.current_ans:
            self.score += 1
            messagebox.showinfo("正确 ✅", "太棒啦，回答正确！")
        else:
            messagebox.showerror("错误 ❌", f"答错咯！正确答案是：{self.current_ans}")
        self.update_score()
        self.next_question()

    # 更新分数展示
    def update_score(self):
        self.label_score.config(text=f"得分：{self.score} / 总答题：{self.total}")

    # 清空分数
    def reset_score(self):
        self.score = 0
        self.total = 0
        self.update_score()
        self.next_question()

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