import tkinter as tk
from tkinter import ttk, scrolledtext

# 这里放一部分唐诗示例，你可以无限加
poems = [
    {"title": "静夜思", "author": "李白",
     "content": "床前明月光，疑是地上霜。\n举头望明月，低头思故乡。"},
    {"title": "春晓", "author": "孟浩然",
     "content": "春眠不觉晓，处处闻啼鸟。\n夜来风雨声，花落知多少。"},
    {"title": "登鹳雀楼", "author": "王之涣",
     "content": "白日依山尽，黄河入海流。\n欲穷千里目，更上一层楼。"},
    {"title": "相思", "author": "王维",
     "content": "红豆生南国，春来发几枝。\n愿君多采撷，此物最相思。"},
    {"title": "咏鹅", "author": "骆宾王",
     "content": "鹅，鹅，鹅，曲项向天歌。\n白毛浮绿水，红掌拨清波。"},
    {"title": "草", "author": "白居易",
     "content": "离离原上草，一岁一枯荣。\n野火烧不尽，春风吹又生。"},
]


class TangPoemApp:
    def __init__(self, root):
        self.root = root
        self.root.title("唐诗三百首")
        self.root.geometry("600x500")

        # 搜索框
        tk.Label(root, text="搜索诗词：", font=("宋体", 12)).pack(pady=5)
        self.search_var = tk.StringVar()
        search_entry = ttk.Entry(
            root, textvariable=self.search_var, font=("宋体", 12), width=40)
        search_entry.pack()
        search_entry.bind("<KeyRelease>", self.search_poem)

        # 列表框
        self.poem_list = tk.Listbox(root, font=("宋体", 12), width=60, height=8)
        self.poem_list.pack(pady=5, fill=tk.BOTH, expand=True)
        self.poem_list.bind("<<ListboxSelect>>", self.show_poem)
        self.load_all_poems()

        # 显示内容
        tk.Label(root, text="诗文内容", font=("宋体", 13, "bold")).pack()
        self.text_area = scrolledtext.ScrolledText(
            root, font=("宋体", 14), width=70, height=12
        )
        self.text_area.pack(pady=5, fill=tk.BOTH, expand=True)

    def load_all_poems(self):
        self.poem_list.delete(0, tk.END)
        for idx, p in enumerate(poems):
            self.poem_list.insert(
                tk.END, f"{idx+1:03d}. {p['title']} - {p['author']}")

    def search_poem(self, event):
        key = self.search_var.get().strip()
        self.poem_list.delete(0, tk.END)
        for idx, p in enumerate(poems):
            if (key in p['title'] or
                key in p['author'] or
                    key in p['content']):
                self.poem_list.insert(
                    tk.END, f"{idx+1:03d}. {p['title']} - {p['author']}")

    def show_poem(self, event):
        selected = self.poem_list.curselection()
        if not selected:
            return
        index = selected[0]
        poem = poems[index]
        content = (
            f"《{poem['title']}》\n"
            f"【唐】{poem['author']}\n\n"
            f"{poem['content']}"
        )
        self.text_area.delete(1.0, tk.END)
        self.text_area.insert(tk.END, content)


if __name__ == "__main__":
    root = tk.Tk()
    app = TangPoemApp(root)
    root.mainloop()
