import tkinter as tk
from tkinter import messagebox
import random

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

        # 变量
        self.mode = tk.StringVar(value="四则运算")
        self.score = 0
        self.total = 0
        self.question = ""
        self.answer = 0

        # 界面组件
        tk.Label(root, text="计算小能手", font=("SimHei", 20)).pack(pady=8)

        # 模式选择
        frame_mode = tk.Frame(root)
        frame_mode.pack()
        tk.Radiobutton(frame_mode, text="四则运算", variable=self.mode, value="四则运算", command=self.new_question, font=("SimHei",12)).grid(row=0,column=0,padx=10)
        tk.Radiobutton(frame_mode, text="九九乘法表", variable=self.mode, value="九九乘法表", command=self.new_question, font=("SimHei",12)).grid(row=0,column=1,padx=10)

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

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

        # 按钮
        frame_btn = tk.Frame(root)
        frame_btn.pack(pady=12)
        tk.Button(frame_btn, text="提交答案", command=self.check_answer, font=("SimHei",12)).grid(row=0,column=0,padx=5)
        tk.Button(frame_btn, text="下一题", command=self.new_question, font=("SimHei",12)).grid(row=0,column=1,padx=5)
        tk.Button(frame_btn, text="重置分数", command=self.reset_score, font=("SimHei",12)).grid(row=0,column=2,padx=5)

        # 分数
        self.label_score = tk.Label(root, text=f"得分：{self.score} / 总题数：{self.total}", font=("SimHei",13))
        self.label_score.pack(pady=5)

        self.new_question()

    # 生成新题目
    def new_question(self):
        if self.mode.get() == "九九乘法表":
            a = random.randint(1,9)
            b = random.randint(1,9)
            self.question = f"{a} × {b} = ?"
            self.answer = a * b
        else:
            # 四则运算，只保留整数结果
            op = random.choice(["+", "-", "*"])
            if op == "+":
                a = random.randint(1,50)
                b = random.randint(1,50)
                self.answer = a + b
            elif op == "-":
                a = random.randint(1,50)
                b = random.randint(1,a)
                self.answer = a - b
            else:
                a = random.randint(1,12)
                b = random.randint(1,12)
                self.answer = a * b
            self.question = f"{a} {op} {b} = ?"

        self.label_q.config(text=self.question)
        self.entry_ans.delete(0, tk.END)
        self.entry_ans.focus()

    # 判断答案
    def check_answer(self, event=None):
        try:
            user_ans = int(self.entry_ans.get())
        except ValueError:
            messagebox.showwarning("提示", "请输入数字！")
            return

        self.total +=1
        if user_ans == self.answer:
            self.score +=1
            messagebox.showinfo("正确 ✅", "太棒啦，回答正确！")
        else:
            messagebox.showerror("错误 ❌", f"不对哦，正确答案是：{self.answer}")

        self.update_score()
        self.new_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.new_question()

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