import tkinter as tk
from tkinter import messagebox
import random

# 主窗口
root = tk.Tk()
root.title("计算小能手 · 数学练习题")
root.geometry("520x380")
root.resizable(False, False)

# 全局变量
mode = tk.StringVar(value="四则运算")  # 当前模式
question = tk.StringVar()    # 题目文本
score = 0
count = 0
right_num = 0
wrong_num = 0
ans_correct = 0             # 正确答案

# 切换模式
def change_mode():
    global score, count, right_num, wrong_num
    score = count = right_num = wrong_num = 0
    refresh_question()
    info_text.set("已切换模式，开始答题！")

# 生成四则运算题目
def make_four():
    a = random.randint(1, 99)
    b = random.randint(1, 99)
    op = random.choice(["+", "-", "*", "/"])
    global ans_correct
    if op == "+":
        ans_correct = a + b
    elif op == "-":
        a, b = max(a, b), min(a, b)
        ans_correct = a - b
    elif op == "*":
        a = random.randint(1, 12)
        b = random.randint(1, 12)
        ans_correct = a * b
    else:
        b = random.randint(1, 12)
        a = b * random.randint(1, 10)
        ans_correct = a // b
    return f"{a} {op} {b} = ?"

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

# 刷新新题目
def refresh_question():
    if mode.get() == "四则运算":
        q = make_four()
    else:
        q = make_multi()
    question.set(q)
    input_ans.delete(0, tk.END)

# 提交答案批改
def submit_answer():
    global score, count, right_num, wrong_num
    count += 1
    try:
        user_ans = int(input_ans.get().strip())
    except ValueError:
        info_text.set("❌ 请输入纯数字答案！")
        return

    if user_ans == ans_correct:
        right_num += 1
        score += 10
        info_text.set(f"✅ 回答正确！当前得分：{score}")
    else:
        wrong_num += 1
        info_text.set(f"❌ 答错啦，正确答案：{ans_correct}")
    # 更新统计
    stat_text.set(f"总题数：{count} | 对：{right_num} | 错：{wrong_num}")
    root.after(800, refresh_question)

# 全部重置清零
def reset_all():
    global score, count, right_num, wrong_num
    score = count = right_num = wrong_num = 0
    stat_text.set("总题数：0 | 对：0 | 错：0")
    info_text.set("数据已清空，开始新练习")
    refresh_question()

# 界面布局
# 顶部模式选择
frame_top = tk.Frame(root)
frame_top.pack(pady=12)
tk.Radiobutton(frame_top, text="四则运算", variable=mode, value="四则运算", command=change_mode, font=("微软雅黑",11)).grid(row=0,column=0,padx=10)
tk.Radiobutton(frame_top, text="九九乘法", variable=mode, value="九九乘法", command=change_mode, font=("微软雅黑",11)).grid(row=0,column=1,padx=10)

# 题目显示
tk.Label(root, textvariable=question, font=("黑体", 32), fg="#b82222").pack(pady=25)

# 答案输入框
frame_input = tk.Frame(root)
frame_input.pack()
tk.Label(frame_input, text="你的答案：", font=("微软雅黑",12)).grid(row=0,column=0)
input_ans = tk.Entry(frame_input, font=("Arial",16), width=10, justify="center")
input_ans.grid(row=0,column=1,padx=8)

# 按钮区
frame_btn = tk.Frame(root)
frame_btn.pack(pady=15)
tk.Button(frame_btn, text="提交答案", command=submit_answer, font=("微软雅黑",11), width=9).grid(row=0,column=0,padx=6)
tk.Button(frame_btn, text="下一题", command=refresh_question, font=("微软雅黑",11), width=9).grid(row=0,column=1,padx=6)
tk.Button(frame_btn, text="重置统计", command=reset_all, font=("微软雅黑",11), width=9).grid(row=0,column=2,padx=6)

# 提示信息
info_text = tk.StringVar(value="选择模式即可开始练习")
tk.Label(root, textvariable=info_text, font=("微软雅黑",12), fg="#226622").pack(pady=8)

# 答题统计
stat_text = tk.StringVar(value="总题数：0 | 对：0 | 错：0")
tk.Label(root, textvariable=stat_text, font=("微软雅黑",11), fg="#333333").pack(pady=10)

# 初始化第一道题
refresh_question()
root.mainloop()