python的专属浪漫-爱心代码

该文章已生成可运行项目,

效果图:
在这里插入图片描述
在这里插入图片描述

import tkinter as tk
import random
import time
import math

tips = [
    "我想你了", "好好吃饭", "别熬夜", "多喝水呀",
    "顺顺利利", "好好爱自己", "保持好心情", "天凉了多穿衣服",
    "充实自己"
]

colors = ["#ffb6c1", "#90ee90", "#87ceeb", "#fffacd", "#ff8ed0"]


root_temp = tk.Tk()
SCREEN_W = root_temp.winfo_screenwidth()
SCREEN_H = root_temp.winfo_screenheight()
root_temp.destroy()

WIN_W = 150
WIN_H = 70

def get_heart_points(num=90):
    points = []
    for i in range(num, 0, -1):
        t = i / num * 2 * math.pi
        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))
        scale = 18
        cx = int(SCREEN_W / 2 + x * scale - WIN_W / 2)
        cy = int(SCREEN_H / 2 + y * scale - WIN_H / 2)
        points.append((cx, cy))
    return points


# 创建弹窗
def create_popup(x, y, text=None):
    win = tk.Toplevel()
    win.geometry(f"{WIN_W}x{WIN_H}+{x}+{y}")
    win.title("")
    win.attributes("-topmost", 1)  # 窗口置顶
    win.resizable(False, False)

    bg = random.choice(colors)
    win.config(bg=bg)
    txt = text if text else random.choice(tips)

    tk.Label(win, text=txt, bg=bg, font=("微软雅黑", 12, "bold")).pack(expand=True)
    return win


def main():
    root = tk.Tk()
    root.withdraw()
    heart_wins = []
    all_wins = []

    # 阶段1:从中间→右上→往下绘制大爱心,形成完整闭环
    heart_points = get_heart_points(90)
    for x, y in heart_points:
        win = create_popup(x, y)
        heart_wins.append(win)
        root.update()
        time.sleep(0.006)

    # 爱心闭环完成,立刻全部消失
    for win in heart_wins:
        if win.winfo_exists():
            win.destroy()
            root.update()

    # 阶段2:直接全屏铺满弹窗
    for _ in range(70):
        x = random.randint(0, SCREEN_W - WIN_W)
        y = random.randint(0, SCREEN_H - WIN_H)
        win = create_popup(x, y)
        all_wins.append(win)
        root.update()
        time.sleep(0.008)

    # 阶段3:关闭全屏弹窗
    interval = 1.0 / len(all_wins) if all_wins else 0
    for win in all_wins:
        if win.winfo_exists():
            win.destroy()
            root.update()
            time.sleep(interval)

    root.quit()


if __name__ == "__main__":
    main()

直接输入运行即可!

本文章已经生成可运行项目
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT=>小脑虎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值