Python实现爱心弹窗的完整教学(附源码)
作者:鸽芷咕
这篇文章主要为大家详细介绍了Python实现爱心弹窗的特效,一段不到 40 行的 Python 代码,在屏幕上依次绽放出爱心形状的弹窗,每一条都是你想对 TA 说的话,有需要的小伙伴可以了解下
一段不到 40 行的 Python 代码,在屏幕上依次绽放出爱心形状的弹窗,每一条都是你想对 TA 说的话。
效果预览

程序运行后会出现三个阶段:
- 爱心绽放 — 100 个小弹窗沿心形曲线依次弹出,每 0.03 秒一个,最终拼成一顆完整的爱心。
- 满屏暴击 — 爱心消失后,弹窗铺满整个屏幕,每个窗口都是一句暖心的叮嘱。
- 温柔收场 — 等待 10 秒后,所有弹窗在 1 秒内优雅地逐个关闭。
按下 空格键 可以随时退出。
核心原理
1. 心形曲线公式
爱心形状来自经典的参数方程:
- x=16sin3(t)
- y=13cos(t)−5cos(2t)−2cos(3t)−cos(4t)
将参数 t 从 0 到 2π 均匀取 100 个点,就能得到一个平滑的心形轮廓。然后将其缩放并居中到屏幕中央。
def heart_points(n, screen_w, screen_h):
points = []
for i in range(n):
t = i / n * 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)
sx = int(screen_w/2 + x*20 - 50) # 缩放 20 倍并居中
sy = int(screen_h/2 - y*20 - 80)
points.append((sx, sy))
return points
2. 弹窗窗口
每个弹窗本质上是一个 tk.Toplevel 小窗口:
def create_window(x, y, tip=None):
win = tk.Toplevel()
win.geometry(f"150x60+{x}+{y}")
win.title("提示")
win.attributes('-topmost', 1) # 始终置顶
text = tip or random.choice(tips) # 随机选取一句暖心话
color = random.choice(colors) # 随机背景色
tk.Label(win, text=text, bg=color,
font=("微软雅黑", 14), width=20, height=3).pack()
return win
-topmost:确保弹窗始终在最前方。- 随机文案:每次从预设列表中随机抽取一条温馨提醒。
- 随机配色:粉色、浅蓝、浅绿等柔和色调,视觉上温馨可爱。
3. 三阶段动画流程
# 阶段一:爱心绽放
for x, y in heart_points(100, sw, sh):
win = create_window(x, y)
hearts.append(win)
root.update()
time.sleep(0.03)
time.sleep(1)
# 销毁爱心弹窗
# 阶段二:满屏暴击
for _ in range(sw//150 * sh//40 + 50):
x = random.randint(0, sw-150)
y = random.randint(0, sh-60)
win = create_window(x, y)
all_windows.append(win)
root.update()
time.sleep(0.005)
time.sleep(10)
# 阶段三:优雅关闭
interval = 1.0 / len(all_windows)
for win in all_windows:
win.destroy()
root.update()
time.sleep(interval)
完整代码(可读版)
import tkinter as tk, random, time, sys, math
hearts, all_wins = [], []
tips = ["多喝水利", "好好爱自己", "好好吃饭", "保持好心情",
"我想你了", "顺顺利利", "别熬夜", "天凉了多穿衣服"]
colors = ["pink", "lightblue", "lightgreen", "lemonchiffon",
"hotpink", "skyblue"]
def heart_points(n, screen_w, screen_h):
"""生成心形曲线上的 n 个屏幕坐标点"""
points = []
for i in range(n):
t = i / n * 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))
sx = int(screen_w / 2 + x * 20 - 50)
sy = int(screen_h / 2 - y * 20 - 80)
sx = max(0, min(sx, screen_w - 150))
sy = max(0, min(sy, screen_h - 60))
points.append((sx, sy))
return points
def create_popup(x, y, tip=None):
"""在指定位置创建一个弹窗"""
win = tk.Toplevel()
win.geometry(f"150x60+{x}+{y}")
win.title("提示")
win.attributes('-topmost', 1)
text = tip or random.choice(tips)
bg = random.choice(colors)
tk.Label(win, text=text, bg=bg,
font=("微软雅黑", 14), width=20, height=3).pack()
win.bind('<space>',
lambda e: [w.destroy() for w in hearts + all_wins] or sys.exit())
return win
def main():
root = tk.Tk()
root.withdraw()
sw = root.winfo_screenwidth()
sh = root.winfo_screenheight()
# ---- 阶段一:爱心绽放 ----
points = heart_points(100, sw, sh)
for i, (x, y) in enumerate(points):
tip = "充实自己" if i == len(points) - 1 else None
win = create_popup(x, y, tip)
hearts.append(win)
root.update()
time.sleep(0.03)
time.sleep(1)
for w in hearts:
if isinstance(w, tk.Toplevel) and w.winfo_exists():
w.destroy()
# ---- 阶段二:满屏暴击 ----
count = sw // 150 * sh // 40 + 50
for _ in range(count):
x = random.randint(0, sw - 150)
y = random.randint(0, sh - 60)
win = create_popup(x, y)
all_wins.append(win)
root.update()
time.sleep(0.005)
time.sleep(10)
# ---- 阶段三:优雅关闭 ----
interval = 1.0 / len(all_wins) if all_wins else 0
for win in all_wins:
if isinstance(win, tk.Toplevel) and win.winfo_exists():
win.destroy()
root.update()
time.sleep(interval)
root.mainloop()
if __name__ == "__main__":
main()
自定义指南
修改文案
编辑 tips 列表即可替换弹窗中显示的文字:
tips = ["自定义文案1", "自定义文案2", "..."]
修改配色
编辑 colors 列表,支持所有 Tkinter 认可的颜色名或十六进制值:
colors = ["#FFB6C1", "#87CEEB", "#DDA0DD", "#98FB98"]
调整爱心大小
修改 heart_points 函数中 x * 20 和 y * 20 的缩放倍数,数字越大爱心越大。
调整弹窗数量
- 爱心弹窗数:修改
heart_points(100, ...)中的100 - 满屏弹窗数:修改
sw // 150 * sh // 40 + 50中的公式
运行方式
确保已安装 Python 3(自带 tkinter),直接运行:
python 爱心弹窗.py
仅依赖 Python 标准库,无需安装任何第三方包。
小结
这个小程序用到的知识点:
| 知识点 | 说明 |
|---|---|
| tkinter | Python 标准 GUI 库 |
| 心形参数方程 | 数学之美 |
| time.sleep + update() | 手动实现逐帧动画 |
| random.choice | 随机选取文案和颜色 |
| -topmost 属性 | 窗口始终置顶 |
不到 40 行代码,就能给 TA 一个小小的惊喜。快去试试吧!
以上就是Python实现爱心弹窗的完整教学(附源码)的详细内容,更多关于Python爱心弹窗的资料请关注脚本之家其它相关文章!
