import tkinter as tk
from tkinter import scrolledtext

# 模拟的唐诗数据（实际可替换为读取外部文件，这里仅展示几首示例）
poems_data = {
    "静夜思": {
        "author": "李白",
        "content": "床前明月光，疑是地上霜。\n举头望明月，低头思故乡。"
    },
    "春晓": {
        "author": "孟浩然",
        "content": "春眠不觉晓，处处闻啼鸟。\n夜来风雨声，花落知多少。"
    },
    "登鹳雀楼": {
        "author": "王之涣",
        "content": "白日依山尽，黄河入海流。\n欲穷千里目，更上一层楼。"
    },
    "相思": {
        "author": "王维",
        "content": "红豆生南国，春来发几枝。\n愿君多采撷，此物最相思。"
    },
    "江雪": {
        "author": "柳宗元",
        "content": "千山鸟飞绝，万径人踪灭。\n孤舟蓑笠翁，独钓寒江雪。"
    },
}

class TangPoetryApp:
    def __init__(self, root):
        self.root = root
        self.root.title("唐诗三百首 - 简易阅读器")
        self.root.geometry("700x500")

        # 左侧列表框（诗题列表）
        self.listbox = tk.Listbox(root, width=20, font=("宋体", 12))
        self.listbox.pack(side=tk.LEFT, fill=tk.Y, padx=5, pady=5)

        # 填充诗题
        for title in poems_data.keys():
            self.listbox.insert(tk.END, title)

        # 右侧显示区域
        self.text_area = scrolledtext.ScrolledText(root, wrap=tk.WORD, font=("宋体", 12))
        self.text_area.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True, padx=5, pady=5)

        # 绑定列表选择事件
        self.listbox.bind('<<ListboxSelect>>', self.show_poem)

    def show_poem(self, event):
        selection = self.listbox.curselection()
        if not selection:
            return
        index = selection[0]
        title = self.listbox.get(index)
        poem = poems_data.get(title)
        if poem:
            display_text = f"《{title}》\n作者：{poem['author']}\n\n{poem['content']}"
            self.text_area.delete(1.0, tk.END)   # 清空之前内容
            self.text_area.insert(tk.END, display_text)

if __name__ == "__main__":
    root = tk.Tk()
    app = TangPoetryApp(root)
    root.mainloop()