boxmoe_header_banner_img

Hello!

加载中

文章导读

Python心形弹窗


avatar
genan 2025-11-05 60

分享一个由python写的创意心形弹窗:
效果图如下:

源码如下:

import tkinter as tk
import random
import math

tips = [
    "天冷多穿衣服", "多喝热水", "要吃热饭", "我想你了",
    "保持微笑~~~(^v^)~~~", "保持好心情呀", "好好爱自己", "梦想成真",
    "别熬夜", "所有的烦恼都消失", "身体倍儿棒", "吃嘛嘛香",
    "每天都要元气满满", "升职加薪!", "期待下次见面"
]

colors = [
    'lightpink', 'skyblue', 'lightgreen', 'lavender',
    'lightyellow', 'plum', 'coral', 'bisque', 'aquamarine',
    'mistyrose', 'honeydew', 'lavenderblush', 'oldlace'
]


def get_screen_resolution():
    """获取屏幕分辨率"""
    root = tk.Tk()
    root.withdraw()
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()
    root.destroy()
    return screen_width, screen_height


def heart_coordinates(num_points):
    """标准心形坐标"""
    points = []
    for i in range(num_points):
        t = 2 * math.pi * i / num_points
        x = 16 * (math.sin(t) ** 3)
        y = 13 * math.cos(t) - 5 * math.cos(2 * t) - 2 * math.cos(3 * t) - math.cos(4 * t)
        points.append((x, y))
    return points


def create_heart_window(x, y, root, screen_width, screen_height):
    """创建单个心形窗口"""
    # 坐标转换
    scale_factor = min(screen_width, screen_height) / 1500
    screen_x = int(x * 30 * scale_factor + screen_width / 2)
    screen_y = int(-y * 30 * scale_factor + screen_height / 2)

    # 创建窗口
    window = tk.Toplevel()
    window.title("你好呀~")
    window.geometry(f"250x100+{screen_x}+{screen_y}")

    # 随机选择颜色和文字
    color = random.choice(colors)
    text = random.choice(tips)

    # 设置窗口样式
    window.configure(background=color)
    label = tk.Label(
        window,
        text=text,
        bg=color,
        font=("微软雅黑", 16, "bold")
    )
    label.pack()

    # 绑定关闭事件
    def on_closing():
        root.destroy()
    window.protocol("WM_DELETE_WINDOW", on_closing)


def create_heart_sequence():
    """创建心形序列窗口"""
    # 获取屏幕信息
    screen_width, screen_height = get_screen_resolution()
    print(f"屏幕分辨率: {screen_width}x{screen_height}")

    # 计算心形坐标
    points = heart_coordinates(60)

    # 创建隐藏的主控制窗口
    root = tk.Tk()
    root.withdraw()

    current_index = 0

    def create_next_window():
        nonlocal current_index
        if current_index < len(points):
            x, y = points[current_index]
            create_heart_window(x, y, root, screen_width, screen_height)
            current_index += 1
            root.after(350, create_next_window)  # 350ms后创建下一个

    # 立即开始生成
    root.after(100, create_next_window)
    root.mainloop()


# 运行心形序列
create_heart_sequence()


评论(0)

查看评论列表

暂无评论


发表评论

表情 颜文字
插入代码