import tkinter as tk
from tkinter import ttk, messagebox

# 单词库：英文:中文释义，你可以自己增删单词
word_list = [
    {"en": "apple", "cn": "苹果"},
    {"en": "banana", "cn": "香蕉"},
    {"en": "computer", "cn": "电脑"},
    {"en": "program", "cn": "程序"},
    {"en": "python", "cn": "蟒蛇；Python语言"},
    {"en": "window", "cn": "窗口"},
    {"en": "function", "cn": "函数"},
    {"en": "variable", "cn": "变量"},
    {"en": "algorithm", "cn": "算法"},
    {"en": "memory", "cn": "内存；记忆"},
    {"en": "keyboard", "cn": "键盘"},
    {"en": "screen", "cn": "屏幕"}
]

# 全局变量
current_index = 0
show_meaning = False
mastered = set()  # 存放已掌握单词下标

def refresh_word():
    """刷新当前单词卡片"""
    if not word_list:
        label_en.config(text="无单词")
        label_cn.config(text="")
        return
    word = word_list[current_index]
    label_en.config(text=word["en"], font=("Arial", 32, "bold"))
    if show_meaning:
        label_cn.config(text=word["cn"], font=("SimHei", 20))
    else:
        label_cn.config(text="???")
    # 更新进度
    label_progress.config(text=f"进度：{current_index+1}/{len(word_list)}")

def toggle_meaning():
    """切换释义显示隐藏"""
    global show_meaning
    show_meaning = not show_meaning
    refresh_word()

def next_word():
    """下一个单词，跳过已掌握"""
    global current_index
    for _ in range(len(word_list)):
        current_index += 1
        if current_index >= len(word_list):
            current_index = 0
        if current_index not in mastered:
            break
    refresh_word()

def prev_word():
    """上一个单词，跳过已掌握"""
    global current_index
    for _ in range(len(word_list)):
        current_index -= 1
        if current_index < 0:
            current_index = len(word_list)-1
        if current_index not in mastered:
            break
    refresh_word()

def mark_mastered():
    """标记当前单词已掌握"""
    global current_index
    mastered.add(current_index)
    messagebox.showinfo("提示", f"【{word_list[current_index]['en']}】标记为已掌握！")
    next_word()

def reset_all():
    """重置所有标记，重新开始"""
    global mastered, current_index, show_meaning
    mastered.clear()
    current_index = 0
    show_meaning = False
    refresh_word()
    messagebox.showinfo("重置", "已清空掌握标记，从头开始！")

# ========== GUI界面 ==========
root = tk.Tk()
root.title("英语单词背诵器")
root.geometry("600x400")
root.resizable(False, False)

# 单词卡片区域
frame_card = tk.Frame(root, relief=tk.GROOVE, bd=2, width=550, height=200)
frame_card.pack(pady=20, padx=20, fill="x")

label_en = tk.Label(frame_card, text="", fg="#222222")
label_en.pack(pady=15)
label_cn = tk.Label(frame_card, text="???", fg="#0066cc")
label_cn.pack(pady=10)

label_progress = tk.Label(root, text="进度：1/10", font=("SimHei",12))
label_progress.pack()

# 按钮区域
frame_btn = tk.Frame(root)
frame_btn.pack(pady=15)

btn_prev = ttk.Button(frame_btn, text="上一个", command=prev_word)
btn_prev.grid(row=0, column=0, padx=6)

btn_toggle = ttk.Button(frame_btn, text="显示/隐藏释义", command=toggle_meaning)
btn_toggle.grid(row=0, column=1, padx=6)

btn_next = ttk.Button(frame_btn, text="下一个", command=next_word)
btn_next.grid(row=0, column=2, padx=6)

btn_mark = ttk.Button(frame_btn, text="标记已掌握", command=mark_mastered)
btn_mark.grid(row=0, column=3, padx=6)

btn_reset = ttk.Button(frame_btn, text="重置", command=reset_all)
btn_reset.grid(row=0, column=4, padx=6)

# 初始化
refresh_word()

root.mainloop()
