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

# 精简唐诗库（24首经典唐诗，可自行继续扩充）
poem_data = [
    {"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但使龙城飞将在，不教胡马度阴山。"},
    {"title":"清明","author":"杜牧","content":"清明时节雨纷纷，路上行人欲断魂。\n借问酒家何处有？牧童遥指杏花村。"},
    {"title":"山行","author":"杜牧","content":"远上寒山石径斜，白云生处有人家。\n停车坐爱枫林晚，霜叶红于二月花。"},
    {"title":"蜂","author":"罗隐","content":"不论平地与山尖，无限风光尽被占。\n采得百花成蜜后，为谁辛苦为谁甜？"},
    {"title":"鹿柴","author":"王维","content":"空山不见人，但闻人语响。\n返景入深林，复照青苔上。"},
    {"title":"寻隐者不遇","author":"贾岛","content":"松下问童子，言师采药去。\n只在此山中，云深不知处。"},
    {"title":"游子吟","author":"孟郊","content":"慈母手中线，游子身上衣。\n临行密密缝，意恐迟迟归。\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("520x620")
        self.root.config(bg="#f5efe0")
        self.index = 0    # 当前诗歌下标
        self.collect_list = [] #收藏列表

        #标题
        tk.Label(root,text="📜唐诗三百首",font=("黑体",24,"bold"),bg="#f5efe0",fg="#8b4513").pack(pady=12)

        #诗文展示框
        frame_show = tk.Frame(root,bg="white",bd=3)
        frame_show.pack(padx=20,pady=5,fill="both",expand=True)

        self.title_var = tk.StringVar(value="标题：")
        tk.Label(frame_show,textvariable=self.title_var,font=("微软雅黑",16,"bold"),bg="white",fg="#662506").pack(pady=5)

        self.author_var = tk.StringVar(value="作者：")
        tk.Label(frame_show,textvariable=self.author_var,font=("微软雅黑",13),bg="white",fg="#555555").pack()

        self.content_text = tk.Text(frame_show,font=("楷体",15),height=12,width=35,bd=0)
        self.content_text.pack(pady=10,padx=10)

        #按钮区域
        frame_btn = tk.Frame(root,bg="#f5efe0")
        frame_btn.pack(pady=12)

        btn_list = [
            ("随机抽诗",self.random_poem,"#4189dd"),
            ("上一首",self.last_poem,"#52b788"),
            ("下一首",self.next_poem,"#52b788"),
            ("收藏本诗",self.collect_poem,"#e67e22"),
            ("查看收藏",self.show_collect,"#9b59b6"),
            ("清空",self.clear_all,"#e74c3c")
        ]

        for idx,(txt,func,color) in enumerate(btn_list):
            b = tk.Button(frame_btn,text=txt,command=func,bg=color,fg="white",
                          font=("黑体",11),width=8,height=2)
            b.grid(row=0,column=idx,padx=3)

        #默认打开第一首
        self.show_poem()

    #刷新展示诗词
    def show_poem(self):
        poem = poem_data[self.index]
        self.title_var.set(f"《{poem['title']}》")
        self.author_var.set(f"作者：{poem['author']}")
        self.content_text.delete(1.0,tk.END)
        self.content_text.insert(tk.END,poem["content"])

    #随机抽取
    def random_poem(self):
        self.index = random.randint(0,len(poem_data)-1)
        self.show_poem()

    #上一首
    def last_poem(self):
        self.index -=1
        if self.index <0:
            self.index = len(poem_data)-1
        self.show_poem()

    #下一首
    def next_poem(self):
        self.index +=1
        if self.index >= len(poem_data):
            self.index = 0
        self.show_poem()

    #收藏
    def collect_poem(self):
        now_p = poem_data[self.index]
        if now_p not in self.collect_list:
            self.collect_list.append(now_p)
            messagebox.showinfo("收藏成功",f"已收藏《{now_p['title']}》")
        else:
            messagebox.showwarning("提示","该诗词已经收藏过啦！")

    #打开收藏弹窗
    def show_collect(self):
        if not self.collect_list:
            messagebox.showinfo("收藏夹","暂无收藏的唐诗，快去收藏吧！")
            return
        msg = "【我的收藏】\n"
        for p in self.collect_list:
            msg +=f"\n《{p['title']}》-{p['author']}\n{p['content']}\n"
        messagebox.showinfo("收藏诗集",msg)

    #清空文本
    def clear_all(self):
        self.title_var.set("标题：")
        self.author_var.set("作者：")
        self.content_text.delete(1.0,tk.END)

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