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

# 唐诗库（精选多首，可继续自行添加）
poem_list = [
    {"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飞流直下三千尺，疑是银河落九天。"},
    {"title": "送元二使安西", "author": "王维", "content": "渭城朝雨浥轻尘，客舍青青柳色新。\n劝君更尽一杯酒，西出阳关无故人。"},
    {"title": "九月九日忆山东兄弟", "author": "王维", "content": "独在异乡为异客，每逢佳节倍思亲。\n遥知兄弟登高处，遍插茱萸少一人。"},
    {"title": "清明", "author": "杜牧", "content": "清明时节雨纷纷，路上行人欲断魂。\n借问酒家何处有？牧童遥指杏花村。"},
    {"title": "咏柳", "author": "贺知章", "content": "碧玉妆成一树高，万条垂下绿丝绦。\n不知细叶谁裁出，二月春风似剪刀。"},
    {"title": "回乡偶书", "author": "贺知章", "content": "少小离家老大回，乡音无改鬓毛衰。\n儿童相见不相识，笑问客从何处来。"}
]

current_index = 0  # 当前显示诗词下标

# 主窗口
root = tk.Tk()
root.title("唐诗三百首")
root.geometry("650x500")  # 窗口大小
root.resizable(False, False)  # 固定窗口大小

# 标题标签
title_label = ttk.Label(root, text="唐诗三百首", font=("微软雅黑", 20, "bold"))
title_label.pack(pady=10)

# 搜索区域
search_frame = ttk.Frame(root)
search_frame.pack(fill=tk.X, padx=20, pady=5)

ttk.Label(search_frame, text="搜索：").grid(row=0, column=0, padx=5)
search_entry = ttk.Entry(search_frame, width=30, font=("微软雅黑", 11))
search_entry.grid(row=0, column=1, padx=5)

# 功能按钮行
btn_frame = ttk.Frame(root)
btn_frame.pack(pady=8)

btn_prev = ttk.Button(btn_frame, text="上一首", width=8)
btn_prev.grid(row=0, column=0, padx=8)

btn_next = ttk.Button(btn_frame, text="下一首", width=8)
btn_next.grid(row=0, column=1, padx=8)

btn_random = ttk.Button(btn_frame, text="随机一首", width=10)
btn_random.grid(row=0, column=2, padx=8)

btn_search = ttk.Button(btn_frame, text="查找", width=8)
btn_search.grid(row=0, column=3, padx=8)

btn_clear = ttk.Button(btn_frame, text="清空", width=8)
btn_clear.grid(row=0, column=4, padx=8)

# 诗词显示文本框（带滚动条）
content_text = scrolledtext.ScrolledText(root, font=("微软雅黑", 14), width=55, height=12)
content_text.pack(padx=20, pady=10)
content_text.config(state=tk.DISABLED)

# 定义功能函数
def show_poem(idx):
    """根据下标展示诗词"""
    global current_index
    if 0 <= idx < len(poem_list):
        current_index = idx
        poem = poem_list[idx]
        content_text.config(state=tk.NORMAL)
        content_text.delete(1.0, tk.END)
        # 拼接内容
        text = f"【{poem['title']}】\n作者：{poem['author']}\n\n{poem['content']}"
        content_text.insert(tk.END, text)
        content_text.config(state=tk.DISABLED)

def prev_poem():
    """上一首"""
    show_poem(current_index - 1)

def next_poem():
    """下一首"""
    show_poem(current_index + 1)

def random_poem():
    """随机诗词"""
    idx = random.randint(0, len(poem_list)-1)
    show_poem(idx)

def search_poem():
    """按标题/作者搜索"""
    key = search_entry.get().strip()
    if not key:
        return
    content_text.config(state=tk.NORMAL)
    content_text.delete(1.0, tk.END)
    find_flag = False
    for p in poem_list:
        if key in p["title"] or key in p["author"]:
            res = f"【{p['title']}】 作者：{p['author']}\n{p['content']}\n" + "-"*40 + "\n\n"
            content_text.insert(tk.END, res)
            find_flag = True
    if not find_flag:
        content_text.insert(tk.END, "未找到相关诗词！")
    content_text.config(state=tk.DISABLED)

def clear_all():
    """清空内容"""
    content_text.config(state=tk.NORMAL)
    content_text.delete(1.0, tk.END)
    search_entry.delete(0, tk.END)
    content_text.config(state=tk.DISABLED)

# 绑定按钮事件
btn_prev.config(command=prev_poem)
btn_next.config(command=next_poem)
btn_random.config(command=random_poem)
btn_search.config(command=search_poem)
btn_clear.config(command=clear_all)

# 初始显示第一首
show_poem(0)

# 运行主循环
root.mainloop()