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

# ===================== 唐诗数据 =====================
poems = [
    {"title": "春晓", "author": "孟浩然", "content": "春眠不觉晓，处处闻啼鸟。夜来风雨声，花落知多少。"},
    {"title": "静夜思", "author": "李白", "content": "床前明月光，疑是地上霜。举头望明月，低头思故乡。"},
    {"title": "登鹳雀楼", "author": "王之涣", "content": "白日依山尽，黄河入海流。欲穷千里目，更上一层楼。"},
    {"title": "相思", "author": "王维", "content": "红豆生南国，春来发几枝。愿君多采撷，此物最相思。"},
    {"title": "咏鹅", "author": "骆宾王", "content": "鹅，鹅，鹅，曲项向天歌。白毛浮绿水，红掌拨清波。"},
    {"title": "草", "author": "白居易", "content": "离离原上草，一岁一枯荣。野火烧不尽，春风吹又生。"},
    {"title": "江雪", "author": "柳宗元", "content": "千山鸟飞绝，万径人踪灭。孤舟蓑笠翁，独钓寒江雪。"},
    {"title": "寻隐者不遇", "author": "贾岛", "content": "松下问童子，言师采药去。只在此山中，云深不知处。"},
    {"title": "登飞来峰", "author": "王安石", "content": "飞来山上千寻塔，闻说鸡鸣见日升。不畏浮云遮望眼，自缘身在最高层。"},
    {"title": "梅花", "author": "王安石", "content": "墙角数枝梅，凌寒独自开。遥知不是雪，为有暗香来。"},
    # 可以自己继续往这里添加更多唐诗
]

# ===================== 主程序界面 =====================
class TangPoemApp:
    def __init__(self, root):
        self.root = root
        self.root.title("📜 唐诗三百首 - 简易版")
        self.root.geometry("650x550")  # 窗口大小

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

        # 搜索框架
        search_frame = ttk.Frame(root)
        search_frame.pack(pady=5)

        ttk.Label(search_frame, text="标题搜索：").grid(row=0, column=0, padx=5)
        self.title_entry = ttk.Entry(search_frame, width=15)
        self.title_entry.grid(row=0, column=1, padx=5)

        ttk.Label(search_frame, text="作者搜索：").grid(row=0, column=2, padx=5)
        self.author_entry = ttk.Entry(search_frame, width=15)
        self.author_entry.grid(row=0, column=3, padx=5)

        # 按钮
        btn_frame = ttk.Frame(root)
        btn_frame.pack(pady=5)
        ttk.Button(btn_frame, text="搜索诗词", command=self.search_poem).grid(row=0, column=0, padx=10)
        ttk.Button(btn_frame, text="显示全部", command=self.show_all).grid(row=0, column=1, padx=10)
        ttk.Button(btn_frame, text="清空内容", command=self.clear_text).grid(row=0, column=2, padx=10)

        # 文本显示框
        self.text_area = scrolledtext.ScrolledText(root, width=70, height=22, font=("微软雅黑", 12))
        self.text_area.pack(pady=10, padx=10)

        # 初始显示全部诗词
        self.show_all()

    # 显示所有唐诗
    def show_all(self):
        self.clear_text()
        for idx, poem in enumerate(poems, 1):
            self.text_area.insert(tk.END, f"【第{idx}首】\n")
            self.text_area.insert(tk.END, f"标题：{poem['title']}\n")
            self.text_area.insert(tk.END, f"作者：{poem['author']}\n")
            self.text_area.insert(tk.END, f"内容：{poem['content']}\n\n")

    # 搜索唐诗
    def search_poem(self):
        title = self.title_entry.get().strip()
        author = self.author_entry.get().strip()
        result = []

        for poem in poems:
            match_title = (title == "" or title in poem["title"])
            match_author = (author == "" or author in poem["author"])
            if match_title and match_author:
                result.append(poem)

        self.clear_text()
        if not result:
            messagebox.showinfo("提示", "没有找到符合条件的诗词！")
            return

        for idx, poem in enumerate(result, 1):
            self.text_area.insert(tk.END, f"【搜索结果 {idx}】\n")
            self.text_area.insert(tk.END, f"标题：{poem['title']}\n")
            self.text_area.insert(tk.END, f"作者：{poem['author']}\n")
            self.text_area.insert(tk.END, f"内容：{poem['content']}\n\n")

    # 清空文本框
    def clear_text(self):
        self.text_area.delete(1.0, tk.END)

# ===================== 运行程序 =====================
if __name__ == "__main__":
    window = tk.Tk()
    app = TangPoemApp(window)
    window.mainloop()