import tkinter as tk
from tkinter import ttk
import random

class WeatherApp:
    def __init__(self, root):
        self.root = root
        self.root.title("天气插画生成器")
        self.root.geometry("520x500")

        # 天气选择
        self.weather_var = tk.StringVar(value="晴天")
        weather_options = ["晴天", "多云", "雨天", "雪天", "雷阵雨"]
        ttk.Label(root, text="选择天气:", font=("Arial", 12)).pack(pady=5)
        self.combo = ttk.Combobox(
            root,
            textvariable=self.weather_var,
            values=weather_options,
            state="readonly",
            font=("Arial", 11)
        )
        self.combo.pack(pady=5)

        self.btn = ttk.Button(root, text="生成插画", command=self.draw_weather)
        self.btn.pack(pady=5)

        # 画布
        self.canvas = tk.Canvas(root, width=500, height=400, bg="lightblue")
        self.canvas.pack(pady=10)

        # 默认绘制晴天
        self.draw_weather()

    def draw_weather(self):
        """根据当前选择的天气绘制插画"""
        self.canvas.delete("all")
        weather = self.weather_var.get()
        if weather == "晴天":
            self.draw_sunny()
        elif weather == "多云":
            self.draw_cloudy()
        elif weather == "雨天":
            self.draw_rainy()
        elif weather == "雪天":
            self.draw_snowy()
        elif weather == "雷阵雨":
            self.draw_thunderstorm()

    # ---------- 绘制辅助函数 ----------
    def draw_cloud(self, x, y, color="white", outline="lightgray"):
        """画一朵云（由三个椭圆组成）"""
        self.canvas.create_oval(x, y, x+50, y+30, fill=color, outline=outline)
        self.canvas.create_oval(x+30, y-10, x+80, y+20, fill=color, outline=outline)
        self.canvas.create_oval(x+60, y, x+110, y+30, fill=color, outline=outline)

    def draw_sun(self, x, y, r=40):
        """画太阳（带光芒）"""
        self.canvas.create_oval(x-r, y-r, x+r, y+r, fill="yellow", outline="orange", width=3)
        # 简单光芒（短线）
        for angle in range(0, 360, 30):
            import math
            rad = math.radians(angle)
            x1 = x + (r+10) * math.cos(rad)
            y1 = y + (r+10) * math.sin(rad)
            x2 = x + (r+20) * math.cos(rad)
            y2 = y + (r+20) * math.sin(rad)
            self.canvas.create_line(x1, y1, x2, y2, fill="orange", width=2)

    def draw_grass(self, y=360):
        """画草地"""
        self.canvas.create_rectangle(0, y, 500, 400, fill="green", outline="")

    # ---------- 各天气绘制 ----------
    def draw_sunny(self):
        self.canvas.create_rectangle(0, 0, 500, 400, fill="skyblue", outline="")
        self.draw_sun(420, 80, 45)
        self.draw_cloud(80, 100)
        self.draw_cloud(250, 140)
        self.draw_grass()

    def draw_cloudy(self):
        self.canvas.create_rectangle(0, 0, 500, 400, fill="lightgray", outline="")
        # 多朵云覆盖天空
        self.draw_cloud(30, 50, color="white", outline="gray")
        self.draw_cloud(180, 80, color="white", outline="gray")
        self.draw_cloud(330, 40, color="white", outline="gray")
        self.draw_cloud(100, 200, color="white", outline="gray")
        self.draw_cloud(280, 180, color="white", outline="gray")
        self.draw_cloud(20, 300, color="white", outline="gray")
        self.draw_cloud(400, 300, color="white", outline="gray")
        self.draw_grass()

    def draw_rainy(self):
        self.canvas.create_rectangle(0, 0, 500, 400, fill="darkgray", outline="")
        # 乌云（深色）
        self.draw_cloud(50, 30, color="gray40", outline="gray20")
        self.draw_cloud(200, 60, color="gray40", outline="gray20")
        self.draw_cloud(350, 20, color="gray40", outline="gray20")
        # 雨滴（斜线）
        for _ in range(60):
            x = random.randint(0, 500)
            y = random.randint(100, 400)
            self.canvas.create_line(x, y, x+8, y+16, fill="blue", width=2)
        # 地面
        self.canvas.create_rectangle(0, 380, 500, 400, fill="gray", outline="")

    def draw_snowy(self):
        self.canvas.create_rectangle(0, 0, 500, 400, fill="white", outline="")
        # 雪花（小圆点）
        for _ in range(100):
            x = random.randint(0, 500)
            y = random.randint(0, 350)
            self.canvas.create_oval(x, y, x+5, y+5, fill="lightblue", outline="")
        # 雪地
        self.canvas.create_rectangle(0, 350, 500, 400, fill="white", outline="lightgray")
        # 雪人
        self.draw_snowman(250, 310)

    def draw_snowman(self, x, y):
        """画一个简单的雪人"""
        # 身体（三个圆）
        self.canvas.create_oval(x-30, y-20, x+30, y+40, fill="white", outline="black")
        self.canvas.create_oval(x-22, y-60, x+22, y-20, fill="white", outline="black")
        self.canvas.create_oval(x-12, y-80, x+12, y-60, fill="white", outline="black")
        # 眼睛
        self.canvas.create_oval(x-8, y-72, x-4, y-68, fill="black")
        self.canvas.create_oval(x+4, y-72, x+8, y-68, fill="black")
        # 胡萝卜鼻子
        self.canvas.create_polygon(x, y-70, x+20, y-65, x, y-60, fill="orange")
        # 帽子
        self.canvas.create_rectangle(x-15, y-85, x+15, y-80, fill="black")
        self.canvas.create_rectangle(x-10, y-95, x+10, y-85, fill="black")

    def draw_thunderstorm(self):
        # 复用雨天背景
        self.canvas.create_rectangle(0, 0, 500, 400, fill="darkgray", outline="")
        self.draw_cloud(50, 30, color="gray40", outline="gray20")
        self.draw_cloud(200, 60, color="gray40", outline="gray20")
        self.draw_cloud(350, 20, color="gray40", outline="gray20")
        # 雨滴
        for _ in range(50):
            x = random.randint(0, 500)
            y = random.randint(100, 400)
            self.canvas.create_line(x, y, x+8, y+16, fill="blue", width=2)
        self.canvas.create_rectangle(0, 380, 500, 400, fill="gray", outline="")
        # 闪电（折线）
        self.canvas.create_polygon(
            150, 100, 170, 150, 160, 150,
            180, 200, 160, 160, 170, 160,
            fill="yellow", outline="orange", width=2
        )

if __name__ == "__main__":
    root = tk.Tk()
    app = WeatherApp(root)
    root.mainloop()