import tkinter as tk

# =======================
# 模拟 300 首唐诗数据（这里先放几首作为演示）
# =======================
POEMS = [
    {
        "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("700x500")
        
        # 当前选中的索引
        self.current_index = 0
        
        # 创建左右分栏的 UI
        self.build_ui()
        self.show_poem()

    def build_ui(self):
        # --- 顶部标题 ---
        top_frame = tk.Frame(self.root, bg="#f0f0f0", height=50)
        top_frame.pack(fill="x")
        
        tk.Label(
            top_frame,
            text="唐诗三百首",
            font=("楷体", 20, "bold"),
            bg="#f0f0f0"
        ).pack(pady=10)

        # --- 中间内容区（左右分栏） ---
        main_frame = tk.Frame(self.root)
        main_frame.pack(fill="both", expand=True, padx=10, pady=5)

        # 左侧：列表框（用于选择古诗）
        left_frame = tk.Frame(main_frame, width=150, bg="#fafafa", relief="groove", bd=2)
        left_frame.pack(side="left", fill="y", padx=(0, 10))
        left_frame.pack_propagate(False) # 固定宽度

        tk.Label(left_frame, text="诗 名 列 表", font=("楷体", 12), bg="#fafafa").pack(pady=5)
        
        self.listbox = tk.Listbox(
            left_frame,
            font=("楷体", 12),
            selectmode="single",
            width=15
        )
        self.listbox.pack(fill="both", expand=True, padx=5, pady=5)
        
        # 绑定点击事件：当用户点击列表中的某首诗时，切换显示
        self.listbox.bind("<<ListboxSelect>>", self.on_list_select)

        # 右侧：诗文显示区
        right_frame = tk.Frame(main_frame, bg="#fff", relief="groove", bd=2)
        right_frame.pack(side="left", fill="both", expand=True)

        self.title_label = tk.Label(
            right_frame,
            font=("楷体", 24, "bold"),
            fg="#222",
            bg="#fff",
            anchor="w" # 左对齐
        )
        self.title_label.pack(pady=20, padx=20)

        self.author_label = tk.Label(
            right_frame,
            font=("楷体", 14),
            fg="#555",
            bg="#fff",
            anchor="w"
        )
        self.author_label.pack(padx=20)

        self.text = tk.Text(
            right_frame,
            font=("楷体", 18),
            fg="#111",
            bg="#fff",
            wrap="word",
            relief="flat",
            height=10
        )
        self.text.pack(fill="both", expand=True, padx=40, pady=20)

        # 填充列表数据
        self.populate_list()

    def populate_list(self):
        """将诗名填入左侧列表"""
        for poem in POEMS:
            self.listbox.insert(tk.END, f"{poem['title']} - {poem['author']}")

    def show_poem(self, index=None):
        """显示指定索引的诗"""
        if index is None:
            index = self.current_index
            
        if 0 <= index < len(POEMS):
            self.current_index = index
            poem = POEMS[index]
            
            self.title_label.config(text=poem["title"])
            self.author_label.config(text=f"【唐】{poem['author']}")
            self.text.delete("1.0", tk.END)
            self.text.insert(tk.END, poem["content"])
            
            # 高亮列表中的当前项
            self.listbox.selection_clear(0, tk.END)
            self.listbox.selection_set(index)
            self.listbox.see(index) # 滚动到可视区域

    def on_list_select(self, event):
        """处理列表点击事件"""
        selection = self.listbox.curselection()
        if selection:
            index = selection[0]
            self.show_poem(index)

# =======================
# 启动程序
# =======================
if __name__ == "__main__":
    root = tk.Tk()
    app = TangPoemApp(root)
    root.mainloop()