import tkinter as tk
from tkinter import ttk, messagebox

# 扩充词库，可自行添加更多单词
dict_cn_en = {
    "你好": "hello",
    "世界": "world",
    "苹果": "apple",
    "电脑": "computer",
    "学校": "school",
    "爱": "love",
    "太阳":"sun",
    "月亮":"moon",
    "水":"water",
    "书":"book"
}
dict_en_cn = {v:k for k,v in dict_cn_en.items()}


def translate_text():
    content = input_box.get("1.0", tk.END).strip()
    if not content:
        messagebox.showwarning("提醒","请输入文字！")
        return

    source_lang = lang1.get()
    target_lang = lang2.get()
    result = ""

    if source_lang == "中文" and target_lang == "英语":
        result = dict_cn_en.get(content, "⚠️词库未收录，离线版不能联网翻译")
    elif source_lang == "英语" and target_lang == "中文":
        result = dict_en_cn.get(content.lower(), "⚠️词库未收录，离线版不能联网翻译")
    else:
        result = "离线版仅支持 中文↔英语"

    output_box.config(state=tk.NORMAL)
    output_box.delete("1.0", tk.END)
    output_box.insert(tk.END, result)
    output_box.config(state=tk.DISABLED)


def clear_data():
    input_box.delete("1.0", tk.END)
    output_box.config(state=tk.NORMAL)
    output_box.delete("1.0", tk.END)
    output_box.config(state=tk.DISABLED)


# 创建窗口
root = tk.Tk()
root.title("离线翻译工具")
root.geometry("600x430")

ttk.Label(root, text="离线翻译软件", font=("微软雅黑", 17)).pack(pady=10)

lang_frame = ttk.Frame(root)
lang_frame.pack()
ttk.Label(lang_frame, text="源语言：").grid(row=0, column=0)
lang1 = ttk.Combobox(lang_frame, values=["中文","英语"], width=10)
lang1.current(0)
lang1.grid(row=0, column=1, padx=5)

ttk.Label(lang_frame, text="目标语言：").grid(row=0, column=2)
lang2 = ttk.Combobox(lang_frame, values=["中文","英语"], width=10)
lang2.current(1)
lang2.grid(row=0, column=3, padx=5)

ttk.Label(root, text="输入文本：").pack(anchor="w", padx=20)
input_box = tk.Text(root, height=6, width=70)
input_box.pack(padx=15, pady=3)

btn_frame = ttk.Frame(root)
btn_frame.pack(pady=5)
ttk.Button(btn_frame, text="开始翻译", command=translate_text).grid(row=0, column=0, padx=10)
ttk.Button(btn_frame, text="清空", command=clear_data).grid(row=0, column=1, padx=10)

ttk.Label(root, text="翻译结果：").pack(anchor="w", padx=20)
output_box = tk.Text(root, height=6, width=70)
output_box.pack(padx=15, pady=3)
output_box.config(state=tk.DISABLED)

root.mainloop()
