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孤舟蓑笠翁，独钓寒江雪。"
    },
    {
        "title": "九月九日忆山东兄弟",
        "author": "王维",
        "content": "独在异乡为异客，每逢佳节倍思亲。\n遥知兄弟登高处，遍插茱萸少一人。"
    }
]

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

        self.index = 0  # 当前诗词序号

        # 左右分栏
        paned = ttk.PanedWindow(root, orient=tk.HORIZONTAL)
        paned.pack(fill=tk.BOTH, expand=True, padx=8, pady=8)

        # 左侧诗词列表
        left_frame = ttk.Frame(paned, width=220)
        paned.add(left_frame, weight=1)

        ttk.Label(left_frame, text="诗词目录", font=("黑体",14)).pack()
        self.listbox = tk.Listbox(left_frame, font=("宋体",12))
        self.listbox.pack(fill=tk.BOTH, expand=True)
        self.listbox.bind("<<ListboxSelect>>", self.on_select)

        # 填充列表
        for p in poems:
            self.listbox.insert(tk.END, p["title"])

        # 右侧展示区域
        right_frame = ttk.Frame(paned)
        paned.add(right_frame, weight=3)

        self.title_var = tk.StringVar()
        self.author_var = tk.StringVar()

        ttk.Label(right_frame, textvariable=self.title_var, font=("黑体",18)).pack(pady=(10,2))
        ttk.Label(right_frame, textvariable=self.author_var, font=("宋体",12), foreground="#555555").pack(pady=(0,10))

        self.text_area = scrolledtext.ScrolledText(right_frame, font=("宋体",14), width=40, height=12)
        self.text_area.pack(padx=10, pady=5, fill=tk.BOTH, expand=True)
        self.text_area.config(state=tk.DISABLED)

        # 按钮栏
        btn_frame = ttk.Frame(right_frame)
        btn_frame.pack(pady=8)

        ttk.Button(btn_frame, text="上一首", command=self.prev_poem).grid(row=0,column=0,padx=6)
        ttk.Button(btn_frame, text="下一首", command=self.next_poem).grid(row=0,column=1,padx=6)

        # 搜索框
        search_frame = ttk.Frame(right_frame)
        search_frame.pack(pady=5)
        ttk.Label(search_frame, text="搜索：").grid(row=0,column=0)
        self.search_var = tk.StringVar()
        search_entry = ttk.Entry(search_frame, textvariable=self.search_var)
        search_entry.grid(row=0,column=1,padx=5)
        ttk.Button(search_frame, text="查找", command=self.search_poem).grid(row=0,column=2)

        # 默认显示第一首
        self.show_poem(0)

    def show_poem(self, idx):
        if 0 <= idx < len(poems):
            self.index = idx
            p = poems[idx]
            self.title_var.set(p["title"])
            self.author_var.set("作者：" + p["author"])
            self.text_area.config(state=tk.NORMAL)
            self.text_area.delete(1.0, tk.END)
            self.text_area.insert(tk.END, p["content"])
            self.text_area.config(state=tk.DISABLED)
            self.listbox.selection_clear(0, tk.END)
            self.listbox.selection_set(idx)
            self.listbox.see(idx)

    def on_select(self, event):
        sel = self.listbox.curselection()
        if sel:
            self.show_poem(sel[0])

    def prev_poem(self):
        if self.index > 0:
            self.show_poem(self.index - 1)

    def next_poem(self):
        if self.index < len(poems)-1:
            self.show_poem(self.index + 1)

    def search_poem(self):
        keyword = self.search_var.get().strip()
        if not keyword:
            return
        for i, p in enumerate(poems):
            if keyword in p["title"] or keyword in p["content"] or keyword in p["author"]:
                self.show_poem(i)
                return

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