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

# 唐诗库，可以自行继续扩充
tang_poems = [
    {"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远芳侵古道，晴翠接荒城。\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("650x520")
        self.root.resizable(False, False)
        self.root.configure(bg="#f8f3e6")

        # 大标题
        title_label = tk.Label(root, text="唐诗三百首", font=("微软雅黑", 24, "bold"), bg="#f8f3e6", fg="#8b4513")
        title_label.pack(pady=12)

        # 按钮区
        btn_frame = tk.Frame(root, bg="#f8f3e6")
        btn_frame.pack(pady=5)

        ttk.Button(btn_frame, text="🎲随机一首", command=self.random_poem).grid(row=0, column=0, padx=8)
        ttk.Button(btn_frame, text="⬅上一首", command=self.prev_poem).grid(row=0, column=1, padx=8)
        ttk.Button(btn_frame, text="下一首➡", command=self.next_poem).grid(row=0, column=2, padx=8)

        # 题目作者显示
        self.info_label = tk.Label(root, text="", font=("微软雅黑", 16), bg="#f8f3e6", fg="#5c3317")
        self.info_label.pack(pady=10)

        # 诗文滚动文本框
        self.text_area = scrolledtext.ScrolledText(root, width=55, height=14, font=("楷体", 15),
                                                    bg="#fffaf0", fg="#2c2c2c", relief=tk.GROOVE)
        self.text_area.pack(padx=20, pady=5)
        self.text_area.configure(state="disabled")

        self.index = 0
        self.show_poem(self.index)

    def show_poem(self, idx):
        poem = tang_poems[idx]
        self.info_label.config(text=f"《{poem['title']}》 —— {poem['author']}")
        self.text_area["state"] = "normal"
        self.text_area.delete(1.0, tk.END)
        self.text_area.insert(tk.END, poem["content"])
        self.text_area["state"] = "disabled"

    def random_poem(self):
        self.index = random.randint(0, len(tang_poems)-1)
        self.show_poem(self.index)

    def next_poem(self):
        self.index += 1
        if self.index >= len(tang_poems):
            self.index = 0
        self.show_poem(self.index)

    def prev_poem(self):
        self.index -= 1
        if self.index < 0:
            self.index = len(tang_poems)-1
        self.show_poem(self.index)


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