import tkinter as tk
from tkinter import ttk

# 唐诗库（精选经典篇目）
tang_poems = [
    {"title":"静夜思","author":"李白","content":"床前明月光，疑是地上霜。举头望明月，低头思故乡。"},
    {"title":"春晓","author":"孟浩然","content":"春眠不觉晓，处处闻啼鸟。夜来风雨声，花落知多少。"},
    {"title":"咏鹅","author":"骆宾王","content":"鹅鹅鹅，曲项向天歌。白毛浮绿水，红掌拨清波。"},
    {"title":"登鹳雀楼","author":"王之涣","content":"白日依山尽，黄河入海流。欲穷千里目，更上一层楼。"},
    {"title":"相思","author":"王维","content":"红豆生南国，春来发几枝。愿君多采撷，此物最相思。"},
    {"title":"悯农","author":"李绅","content":"锄禾日当午，汗滴禾下土。谁知盘中餐，粒粒皆辛苦。"},
    {"title":"望庐山瀑布","author":"李白","content":"日照香炉生紫烟，遥看瀑布挂前川。飞流直下三千尺，疑是银河落九天。"},
    {"title":"绝句","author":"杜甫","content":"两个黄鹂鸣翠柳，一行白鹭上青天。窗含西岭千秋雪，门泊东吴万里船。"},
    {"title":"送元二使安西","author":"王维","content":"渭城朝雨浥轻尘，客舍青青柳色新。劝君更尽一杯酒，西出阳关无故人。"},
    {"title":"九月九日忆山东兄弟","author":"王维","content":"独在异乡为异客，每逢佳节倍思亲。遥知兄弟登高处，遍插茱萸少一人。"}
]

class TangPoemApp:
    def __init__(self, root):
        self.root = root
        self.root.title("唐诗三百首")
        self.root.geometry("550x450")
        self.root.resizable(False, False)

        # 标题
        tk.Label(root, text="唐诗三百首", font=("黑体", 22, "bold"), fg="#c0392b").pack(pady=10)

        # 搜索框
        self.search_var = tk.StringVar()
        tk.Entry(root, textvariable=self.search_var, font=("宋体",12), width=35).place(x=100,y=60)
        tk.Button(root, text="搜索诗词", command=self.search_poem, bg="#3498db", fg="white").place(x=360,y=58)

        # 列表框
        tk.Label(root,text="诗词目录",font=("宋体",13)).place(x=20,y=100)
        self.poem_list = tk.Listbox(root, width=18, height=16, font=("宋体",11))
        self.poem_list.place(x=20,y=125)
        self.poem_list.bind("<<ListboxSelect>>",self.show_poem)

        # 诗文展示区域
        tk.Label(root,text="诗词内容",font=("宋体",13)).place(x=220,y=100)
        self.show_text = tk.Text(root, width=32, height=16, font=("宋体",14))
        self.show_text.place(x=220,y=125)

        # 初始化列表
        self.init_list(tang_poems)

    def init_list(self,poem_data):
        self.poem_list.delete(0,tk.END)
        for p in poem_data:
            self.poem_list.insert(tk.END,f"{p['title']} - {p['author']}")

    def show_poem(self,event):
        idx = self.poem_list.curselection()
        if not idx:
            return
        poem = tang_poems[idx[0]]
        self.show_text.delete(1.0,tk.END)
        self.show_text.insert(tk.END,f"《{poem['title']}》\n作者：{poem['author']}\n\n{poem['content']}")

    def search_poem(self):
        key = self.search_var.get().strip()
        if not key:
            self.init_list(tang_poems)
            return
        res = [p for p in tang_poems if key in p["title"] or key in p["author"] or key in p["content"]]
        self.init_list(res)

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