import tkinter as tk
from tkinter import ttk,scrolledtext

# 唐诗库
poem_list = [
    {"title":"静夜思","author":"李白","content":"床前明月光，\n疑是地上霜。\n举头望明月，\n低头思故乡。"},
    {"title":"春晓","author":"孟浩然","content":"春眠不觉晓，\n处处闻啼鸟。\n夜来风雨声，\n花落知多少。"},
    {"title":"登鹳雀楼","author":"王之涣","content":"白日依山尽，\n黄河入海流。\n欲穷千里目，\n更上一层楼。"},
    {"title":"相思","author":"王维","content":"红豆生南国，\n春来发几枝。\n愿君多采撷，\n此物最相思。"},
    {"title":"咏鹅","author":"骆宾王","content":"鹅，鹅，鹅，\n曲项向天歌。\n白毛浮绿水，\n红掌拨清波。"},
    {"title":"江雪","author":"柳宗元","content":"千山鸟飞绝，\n万径人踪灭。\n孤舟蓑笠翁，\n独钓寒江雪。"},
    {"title":"鹿柴","author":"王维","content":"空山不见人，\n但闻人语响。\n返景入深林，\n复照青苔上。"},
    {"title":"九月九日忆山东兄弟","author":"王维","content":"独在异乡为异客，\n每逢佳节倍思亲。\n遥知兄弟登高处，\n遍插茱萸少一人。"},
    {"title":"望庐山瀑布","author":"李白","content":"日照香炉生紫烟，\n遥看瀑布挂前川。\n飞流直下三千尺，\n疑是银河落九天。"},
    {"title":"黄鹤楼送孟浩然之广陵","author":"李白","content":"故人西辞黄鹤楼，\n烟花三月下扬州。\n孤帆远影碧空尽，\n唯见长江天际流。"}
]

current_index = 0

# 加载诗歌
def load_poem(idx):
    global current_index
    if 0 <= idx < len(poem_list):
        current_index = idx
        poem = poem_list[idx]
        title_var.set(poem["title"])
        author_var.set("作者："+poem["author"])
        text_area.delete(1.0,tk.END)
        text_area.insert(tk.END,poem["content"])

# 上一首
def prev_poem():
    if current_index > 0:
        load_poem(current_index - 1)

# 下一首
def next_poem():
    if current_index < len(poem_list)-1:
        load_poem(current_index + 1)

# 点击目录跳转到诗歌
def on_listbox_click(event):
    sel = listbox.curselection()
    if sel:
        load_poem(sel[0])

# ---------------------- 创建窗口 ----------------------
root = tk.Tk()
root.title("唐诗三百首阅读器")
root.geometry("720x520")
root.configure(bg="#f8efe0")

# 左右分栏
left_frame = tk.Frame(root,bg="#e8d9c2",width=200)
left_frame.pack(side=tk.LEFT,fill=tk.BOTH,padx=5,pady=5)

right_frame = tk.Frame(root,bg="#f8efe0")
right_frame.pack(side=tk.RIGHT,fill=tk.BOTH,expand=True,padx=5,pady=5)

# 左侧标题目录
tk.Label(left_frame,text="诗歌目录",font=("微软雅黑",14,"bold"),bg="#e8d9c2").pack(pady=8)
listbox = tk.Listbox(left_frame,font=("微软雅黑",11))
for p in poem_list:
    listbox.insert(tk.END,p["title"])
listbox.pack(fill=tk.BOTH,expand=True,padx=5,pady=5)
listbox.bind("<<ListboxSelect>>",on_listbox_click)

# 右侧诗文标题
title_var = tk.StringVar()
author_var = tk.StringVar()
tk.Label(right_frame,textvariable=title_var,font=("微软雅黑",20,"bold"),bg="#f8efe0").pack(pady=8)
tk.Label(right_frame,textvariable=author_var,font=("微软雅黑",12),bg="#f8efe0").pack()

# 诗文文本框
text_area = scrolledtext.ScrolledText(right_frame,font=("微软雅黑",16),width=35,height=12,bg="#fffaf0")
text_area.pack(pady=10,padx=10)

# 按钮栏
btn_frame = tk.Frame(right_frame,bg="#f8efe0")
btn_frame.pack(pady=5)
tk.Button(btn_frame,text="◀ 上一首",command=prev_poem,font=("微软雅黑",12),bg="#d4b886").grid(row=0,column=0,padx=15)
tk.Button(btn_frame,text="下一首 ▶",command=next_poem,font=("微软雅黑",12),bg="#d4b886").grid(row=0,column=1,padx=15)

# 默认打开第一首
load_poem(0)

root.mainloop()
