import tkinter as tk
from tkinter import ttk, scrolledtext, messagebox

# 内置唐诗数据库（精选唐诗三百首代表篇目）
tang_poems = [
    {"title": "静夜思", "author": "李白", "content": "床前明月光，疑是地上霜。\n举头望明月，低头思故乡。", "note": "诗人客居他乡，望月思念家乡"},
    {"title": "春晓", "author": "孟浩然", "content": "春眠不觉晓，处处闻啼鸟。\n夜来风雨声，花落知多少。", "note": "描绘春日清晨生机与惜春之情"},
    {"title": "登鹳雀楼", "author": "王之涣", "content": "白日依山尽，黄河入海流。\n欲穷千里目，更上一层楼。", "note": "登高望远，蕴含积极进取哲理"},
    {"title": "咏鹅", "author": "骆宾王", "content": "鹅，鹅，鹅，曲项向天歌。\n白毛浮绿水，红掌拨清波。", "note": "孩童视角描写白鹅戏水画面"},
    {"title": "相思", "author": "王维", "content": "红豆生南国，春来发几枝。\n愿君多采撷，此物最相思。", "note": "借红豆寄托思念友人之情"},
    {"title": "九月九日忆山东兄弟", "author": "王维", "content": "独在异乡为异客，每逢佳节倍思亲。\n遥知兄弟登高处，遍插茱萸少一人。", "note": "重阳节思乡，千古思乡名句"},
    {"title": "望庐山瀑布", "author": "李白", "content": "日照香炉生紫烟，遥看瀑布挂前川。\n飞流直下三千尺，疑是银河落九天。", "note": "夸张手法描写庐山瀑布雄伟"},
    {"title": "早发白帝城", "author": "李白", "content": "朝辞白帝彩云间，千里江陵一日还。\n两岸猿声啼不住，轻舟已过万重山。", "note": "遇赦东归，轻快愉悦的心境"},
    {"title": "绝句·两个黄鹂鸣翠柳", "author": "杜甫", "content": "两个黄鹂鸣翠柳，一行白鹭上青天。\n窗含西岭千秋雪，门泊东吴万里船。", "note": "四句四景，工整优美的写景诗"},
    {"title": "春夜喜雨", "author": "杜甫", "content": "好雨知时节，当春乃发生。\n随风潜入夜，润物细无声。\n野径云俱黑，江船火独明。\n晓看红湿处，花重锦官城。", "note": "赞美春雨滋润万物，充满生机"},
    {"title": "赋得古原草送别", "author": "白居易", "content": "离离原上草，一岁一枯荣。\n野火烧不尽，春风吹又生。\n远芳侵古道，晴翠接荒城。\n又送王孙去，萋萋满别情。", "note": "野草顽强生命力，借草抒发离别"},
    {"title": "清明", "author": "杜牧", "content": "清明时节雨纷纷，路上行人欲断魂。\n借问酒家何处有？牧童遥指杏花村。", "note": "清明雨中羁旅之人的淡淡愁绪"},
    {"title": "山行", "author": "杜牧", "content": "远上寒山石径斜，白云生处有人家。\n停车坐爱枫林晚，霜叶红于二月花。", "note": "秋日山林红叶美景，格调明朗"},
    {"title": "江雪", "author": "柳宗元", "content": "千山鸟飞绝，万径人踪灭。\n孤舟蓑笠翁，独钓寒江雪。", "note": "极致清冷雪景，寄托孤傲心境"},
    {"title": "游子吟", "author": "孟郊", "content": "慈母手中线，游子身上衣。\n临行密密缝，意恐迟迟归。\n谁言寸草心，报得三春晖。", "note": "歌颂伟大无私的母爱"},
]


class TangPoemApp:
    def __init__(self, root):
        self.root = root
        self.root.title("唐诗三百首 阅览器")
        self.root.geometry("900x650")  # 窗口大小
        self.poem_list = tang_poems
        self.current_index = 0  # 当前选中诗词下标

        # 顶部搜索栏
        self.top_frame = ttk.Frame(root, padding=10)
        self.top_frame.pack(fill=tk.X)
        ttk.Label(self.top_frame, text="搜索诗人：").pack(side=tk.LEFT)
        self.search_var = tk.StringVar()
        self.search_entry = ttk.Entry(self.top_frame, textvariable=self.search_var, width=30)
        self.search_entry.pack(side=tk.LEFT, padx=5)
        ttk.Button(self.top_frame, text="搜索", command=self.search_author).pack(side=tk.LEFT)
        ttk.Button(self.top_frame, text="重置全部", command=self.reset_all).pack(side=tk.LEFT, padx=10)

        # 主体左右分栏
        self.main_frame = ttk.Frame(root, padding=10)
        self.main_frame.pack(fill=tk.BOTH, expand=True)

        # 左侧诗词目录列表
        self.left_frame = ttk.Frame(self.main_frame, width=180)
        self.left_frame.pack(side=tk.LEFT, fill=tk.BOTH)
        ttk.Label(self.left_frame, text="诗词目录", font=("黑体", 12, "bold")).pack()
        # 修复：删除 Treeview 构造里的 width 参数
        self.poem_tree = ttk.Treeview(self.left_frame, columns=("author",), show="tree headings")
        self.poem_tree.heading("#0", text="诗题")
        self.poem_tree.heading("author", text="作者")
        self.poem_tree.column("#0", width=100)
        self.poem_tree.column("author", width=70)
        scroll_bar = ttk.Scrollbar(self.left_frame, orient=tk.VERTICAL, command=self.poem_tree.yview)
        self.poem_tree.configure(yscrollcommand=scroll_bar.set)
        scroll_bar.pack(side=tk.RIGHT, fill=tk.Y)
        self.poem_tree.pack(fill=tk.BOTH, expand=True)
        self.poem_tree.bind("<<TreeviewSelect>>", self.select_poem)

        # 右侧诗词展示区
        self.right_frame = ttk.Frame(self.main_frame, padding=(15, 0, 0, 0))
        self.right_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)
        # 标题作者
        self.title_label = ttk.Label(self.right_frame, text="", font=("黑体", 16, "bold"))
        self.title_label.pack()
        self.author_label = ttk.Label(self.right_frame, text="", font=("宋体", 12))
        self.author_label.pack(pady=5)
        # 诗词正文
        ttk.Label(self.right_frame, text="诗文：", font=("黑体", 11, "bold")).pack(anchor="w")
        self.content_text = scrolledtext.ScrolledText(self.right_frame, height=12, font=("宋体", 13))
        self.content_text.pack(fill=tk.BOTH, expand=True)
        # 注释区域
        ttk.Label(self.right_frame, text="注释赏析：", font=("黑体", 11, "bold")).pack(anchor="w", pady=(10, 0))
        self.note_text = tk.Text(self.right_frame, height=3, font=("宋体", 11))
        self.note_text.pack(fill=tk.X)

        # 底部翻页按钮
        self.bottom_frame = ttk.Frame(root, padding=10)
        self.bottom_frame.pack(fill=tk.X)
        ttk.Button(self.bottom_frame, text="上一首", command=self.prev_poem).pack(side=tk.LEFT, padx=20)
        ttk.Button(self.bottom_frame, text="下一首", command=self.next_poem).pack(side=tk.LEFT)
        ttk.Label(self.bottom_frame, text=f"共{len(self.poem_list)}首唐诗", foreground="#555555").pack(side=tk.RIGHT, padx=20)

        # 初始化加载目录
        self.refresh_tree()
        self.show_poem(0)

    # 刷新左侧列表
    def refresh_tree(self):
        # 清空原有数据
        for item in self.poem_tree.get_children():
            self.poem_tree.delete(item)
        # 插入所有诗词
        for idx, poem in enumerate(self.poem_list):
            self.poem_tree.insert("", tk.END, iid=str(idx), text=poem["title"], values=(poem["author"],))

    # 选中列表诗词展示
    def select_poem(self, event):
        sel = self.poem_tree.selection()
        if not sel:
            return
        self.current_index = int(sel[0])
        self.show_poem(self.current_index)

    # 展示指定序号诗词
    def show_poem(self, idx):
        poem = self.poem_list[idx]
        # 清空文本框
        self.content_text.delete(1.0, tk.END)
        self.note_text.delete(1.0, tk.END)
        # 填充内容
        self.title_label.config(text=f"《{poem['title']}》")
        self.author_label.config(text=f"作者：{poem['author']}")
        self.content_text.insert(1.0, poem["content"])
        self.note_text.insert(1.0, poem["note"])

    # 上一首
    def prev_poem(self):
        if self.current_index > 0:
            self.current_index -= 1
            self.poem_tree.selection_set(str(self.current_index))
            self.show_poem(self.current_index)
        else:
            messagebox.showinfo("提示", "已经是第一首啦！")

    # 下一首
    def next_poem(self):
        if self.current_index < len(self.poem_list)-1:
            self.current_index += 1
            self.poem_tree.selection_set(str(self.current_index))
            self.show_poem(self.current_index)
        else:
            messagebox.showinfo("提示", "已经是最后一首啦！")

    # 按诗人搜索
    def search_author(self):
        keyword = self.search_var.get().strip()
        if not keyword:
            messagebox.showwarning("警告", "请输入诗人姓名！")
            return
        # 筛选匹配诗人
        filter_poems = [p for p in tang_poems if keyword in p["author"]]
        if not filter_poems:
            messagebox.showinfo("结果", f"未找到【{keyword}】的诗作")
            return
        self.poem_list = filter_poems
        self.current_index = 0
        self.refresh_tree()
        self.show_poem(0)

    # 重置显示全部诗词
    def reset_all(self):
        self.search_var.set("")
        self.poem_list = tang_poems
        self.current_index = 0
        self.refresh_tree()
        self.show_poem(0)


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