网站Logo 来自星辰的 IOT筑梦师

浪漫表白弹窗弹幕(根目录放置图片即可)

huxia
8
2025-12-27
import tkinter as tk
import random
from PIL import Image, ImageTk
import sys
import os

# 🌸浪漫祝福语句(含彩蛋)
BLESSINGS = [
    "祝你每天都像阳光一样灿烂!", "祝你微笑常驻,心事都轻盈!",
    "祝你被世界温柔以待!", "祝你心中有爱,眼里有光!",
    "祝你遇见的每一件事都美好!", "祝你梦里有花,醒来有暖!",
    "祝你生活甜如蜜,快乐满溢!", "祝你每天都被幸福拥抱!",
    "祝你心情如春风般柔软!", "祝你眼里星光闪烁,心里暖洋洋!",
    "祝你拥有甜蜜的惊喜和温暖的陪伴!", "祝你笑容像夏花一样绚烂!",
    "祝你每天都是浪漫的小冒险!", "祝你每一刻都心花怒放!",
    "祝你平安喜乐,温暖常伴!", "祝你被幸运和爱包围!",
    "祝你每一天都像诗一样美!", "祝你快乐像气泡一样轻盈!",
    "祝你梦想都开出花来!", "祝你生活如画,温柔满溢!",
    "祝你幸福像星星一样闪烁!", "祝你微笑像花儿一样盛开!",
    "我喜欢你"  # 彩蛋
]

# 参数
POPUP_INTERVAL = (200, 250)
POPUP_DURATION = 3000
HEART_COUNT = 1
MAX_POPUPS = 30
FINAL_DELAY = 5000
FINAL_TO_IMAGE_DELAY = 10000  # 弹窗结束后进入图片轮播延迟

root = tk.Tk()
root.withdraw()
screen_w = root.winfo_screenwidth()
screen_h = root.winfo_screenheight()

active_popups = 0


def random_color():
    return "#%06x" % random.randint(0, 0xFFFFFF)


def fade_in(window):
    window.attributes('-alpha', 1)


def fade_out(window):
    window.attributes('-alpha', 0)


def create_heart(x, y):
    """爱心动画"""
    top = tk.Toplevel(root)
    top.overrideredirect(True)
    size = random.randint(15, 25)
    color = random.choice(["#ff4d4d", "#ff99cc", "#ff6699", "#ff3366"])
    canvas = tk.Canvas(top, width=size, height=size, bg="black", highlightthickness=0)
    canvas.pack()
    coords = [size / 2, size, size, size / 2, size * 0.75, 0,
              size / 2, size / 4, size * 0.25, 0, 0, size / 2]
    canvas.create_polygon(coords, fill=color, smooth=True)
    top.geometry(f"{size}x{size}+{x}+{y}")
    top.attributes("-topmost", True)

    def float_up(step=0):
        if step < 20:
            top.geometry(f"+{x}+{y - step * 3}")
            top.attributes('-alpha', max(0, 1 - step / 20))
            top.after(40, lambda: float_up(step + 1))
        else:
            top.destroy()

    float_up()


def show_image_slideshow(delay=2000):
    """扫描根目录 PNG/JPG 图片并轮播"""
    image_list = [f for f in os.listdir('.') if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
    if not image_list:
        print("没有找到图片文件")
        return

    top = tk.Toplevel(root)
    top.overrideredirect(True)
    top.attributes("-topmost", True)
    top.configure(bg="black")

    label = tk.Label(top, bg="black")
    label.pack()

    index = 0
    photos = []

    def show_next():
        nonlocal index
        img_path = image_list[index]
        img = Image.open(img_path)

        # 保持比例缩放
        screen_w = root.winfo_screenwidth()
        screen_h = root.winfo_screenheight()
        max_w, max_h = int(screen_w * 0.8), int(screen_h * 0.8)
        ratio = min(max_w / img.width, max_h / img.height)
        new_size = (int(img.width * ratio), int(img.height * ratio))
        img = img.resize(new_size)
        photo = ImageTk.PhotoImage(img)
        photos.append(photo)
        label.config(image=photo)

        # 居中显示
        x = (screen_w - new_size[0]) // 2
        y = (screen_h - new_size[1]) // 2
        top.geometry(f"{new_size[0]}x{new_size[1]}+{x}+{y}")

        index = (index + 1) % len(image_list)
        top.after(delay, show_next)

    show_next()


def create_popup():
    """随机弹出浪漫祝福"""
    global active_popups
    if active_popups >= MAX_POPUPS:
        root.after(POPUP_INTERVAL[0], create_popup)
        return

    w, h = random.randint(200, 280), random.randint(80, 120)
    x = random.randint(0, screen_w - w)
    y = random.randint(0, screen_h - h)
    bg = random_color()

    top = tk.Toplevel(root)
    top.overrideredirect(True)
    top.attributes("-topmost", True)
    top.attributes("-alpha", 0)
    top.geometry(f"{w}x{h}+{x}+{y}")
    top.configure(bg=bg)

    label = tk.Label(top, text=random.choice(BLESSINGS), bg=bg, fg="white",
                     font=("微软雅黑", random.randint(14, 20), "bold"))
    label.pack(expand=True, fill="both")

    fade_in(top)
    active_popups += 1

    def destroy_popup():
        global active_popups
        fade_out(top)
        for _ in range(HEART_COUNT):
            create_heart(x + w // 2, y + h // 2)
        top.destroy()
        active_popups -= 1

    top.after(POPUP_DURATION, destroy_popup)
    delay = random.randint(*POPUP_INTERVAL)
    root.after(delay, create_popup)


# 启动弹窗
create_popup()
# 弹窗结束后播放幻灯片
root.after(FINAL_TO_IMAGE_DELAY, show_image_slideshow)
root.mainloop()

动物装饰