import tkinter as tk
from tkinter import ttk, messagebox
from PIL import Image, ImageDraw, ImageTk
import math

# 画布尺寸
WIDTH = 600
HEIGHT = 400

class WeatherPainter:
    def __init__(self, root):
        self.root = root
        self.root.title("简易天气插画生成器")
        self.root.geometry(f"{WIDTH+40}x{HEIGHT+120}")

        # 变量
        self.weather_type = tk.StringVar(value="晴天")
        self.weather_list = ["晴天", "多云", "阴天", "下雨", "下雪", "雷雨"]

        # 创建UI
        self.create_widgets()
        # 初始化画布图像
        self.img = Image.new("RGB", (WIDTH, HEIGHT), "#87CEEB")
        self.draw = ImageDraw.Draw(self.img)
        self.render_weather()

    def create_widgets(self):
        # 顶部控制栏
        frame_top = tk.Frame(self.root)
        frame_top.pack(pady=8)

        tk.Label(frame_top, text="选择天气：").pack(side=tk.LEFT, padx=5)
        combo = ttk.Combobox(frame_top, textvariable=self.weather_type, values=self.weather_list, width=10)
        combo.pack(side=tk.LEFT, padx=5)
        combo.bind("<<ComboboxSelected>>", lambda e: self.render_weather())

        tk.Button(frame_top, text="刷新画面", command=self.render_weather).pack(side=tk.LEFT, padx=5)
        tk.Button(frame_top, text="保存图片", command=self.save_image).pack(side=tk.LEFT, padx=5)

        # 画布显示区域
        self.canvas_label = tk.Label(self.root)
        self.canvas_label.pack()

    def draw_sun(self):
        """绘制太阳"""
        cx, cy = 100, 80
        r = 35
        self.draw.ellipse((cx-r, cy-r, cx+r, cy+r), fill="#FFDD00")
        # 太阳光晕
        for i in range(12):
            angle = math.radians(i * 30)
            x1 = cx + math.cos(angle) * (r+5)
            y1 = cy + math.sin(angle) * (r+5)
            x2 = cx + math.cos(angle) * (r+22)
            y2 = cy + math.sin(angle) * (r+22)
            self.draw.line((x1, y1, x2, y2), fill="#FFDD00", width=4)

    def draw_cloud(self, x, y, scale=1):
        """绘制云朵"""
        r = int(22 * scale)
        color = (255,255,255)
        self.draw.ellipse((x-r, y-r, x+r, y+r), fill=color)
        self.draw.ellipse((x+r*0.75, y-r*0.6, x+r*2, y+r*0.6), fill=color)
        self.draw.ellipse((x-r*1.2, y-r*0.5, x-r*0.1, y+r*0.7), fill=color)
        self.draw.ellipse((x+r*0.3, y-r*1, x+r*1.4, y+r*0.3), fill=color)

    def draw_rain(self):
        """雨滴"""
        for i in range(60):
            x = 30 + (i * 17) % WIDTH
            y = 120 + (i * 23) % (HEIGHT-120)
            self.draw.line((x, y, x-3, y+12), fill="#77aaff", width=2)

    def draw_snow(self):
        """雪花"""
        for i in range(45):
            x = 20 + (i * 21) % WIDTH
            y = 110 + (i * 19) % (HEIGHT-110)
            r = 3
            self.draw.ellipse((x-r, y-r, x+r, y+r), fill="white")

    def draw_lightning(self):
        """闪电"""
        pts = [
            (420, 90),
            (395, 150),
            (425, 152),
            (380, 230),
            (415, 235),
            (370, 300)
        ]
        self.draw.line(pts, fill="#fff266", width=6)

    def render_weather(self):
        weather = self.weather_type.get()
        # 重置画布
        if weather == "晴天":
            bg = "#87CEEB"
        elif weather == "多云":
            bg = "#94c7ed"
        elif weather == "阴天":
            bg = "#98a8b8"
        elif weather in ("下雨", "雷雨"):
            bg = "#647788"
        elif weather == "下雪":
            bg = "#b0c4d2"
        else:
            bg = "#87CEEB"

        self.img = Image.new("RGB", (WIDTH, HEIGHT), bg)
        self.draw = ImageDraw.Draw(self.img)

        # 地面简单线条
        self.draw.rectangle((0, HEIGHT-60, WIDTH, HEIGHT), fill="#558855")

        if weather == "晴天":
            self.draw_sun()

        elif weather == "多云":
            self.draw_sun()
            self.draw_cloud(260, 90, scale=1.1)
            self.draw_cloud(440, 120, scale=0.85)

        elif weather == "阴天":
            self.draw_cloud(140, 85, scale=1.3)
            self.draw_cloud(320, 110, scale=1.1)
            self.draw_cloud(480, 92, scale=0.95)

        elif weather == "下雨":
            self.draw_cloud(180, 75, scale=1.2)
            self.draw_cloud(360, 90, scale=1.0)
            self.draw_rain()

        elif weather == "下雪":
            self.draw_cloud(200, 80, scale=1.15)
            self.draw_cloud(380, 95, scale=0.95)
            self.draw_snow()

        elif weather == "雷雨":
            self.draw_cloud(160, 70, scale=1.3)
            self.draw_cloud(340, 88, scale=1.1)
            self.draw_rain()
            self.draw_lightning()

        # 更新tk显示
        self.tk_img = ImageTk.PhotoImage(self.img)
        self.canvas_label.config(image=self.tk_img)

    def save_image(self):
        try:
            save_name = f"天气插画_{self.weather_type.get()}.png"
            self.img.save(save_name)
            messagebox.showinfo("保存成功", f"图片已保存为：{save_name}")
        except Exception as e:
            messagebox.showerror("保存失败", str(e))

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