PYTHON · 爱心

屏幕上开满会说话的爱心

一份 tkinter 写的表白脚本:先用 200 个小窗口沿参数方程排出一颗心,再铺满整屏, 每个气泡里随机冒一句话。可以自己在网页上先看效果,也可以下载跑原版。

💡
运行方式:装了 Python 3 的机器上双击即可(tkinter 是标准库,Windows 版 Python 自带)。 脚本会瞬间弹出大量顶层窗口,屏幕上按住 空格 可一次性关掉全部并退出。

玩法与参数

200 个点

heart_points(200, …) 里的 200 是心形上的气泡数,改大更密、耗时更久。

0.05 秒

循环里的 time.sleep(0.05),是心形一笔一笔画出来的速度。

+50 波

count + 50 控制最后铺满屏幕的气泡数量,越大越热闹、也越吃内存。

文案

tips 列表就是气泡里的句子;最后一个心写死「我喜欢你」。

配色

colors 里是 tkinter 认得的颜色名,换成 #ff7a59 这类十六进制也行。

退出

每个窗口都绑了 空格:销毁所有窗口后 sys.exit()。任务管理器也能杀死它。

原文件在 assets/python/爱心.py,网页版是同一套参数方程的 canvas 复刻(assets/js/love.js), 逻辑一一对应,改了参数两边都能对上。

爱心.py 源码

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):
    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(200, sw, sh)          # 200 指形成爱心的方格数,可以改
    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.05)                        # 形成爱心时间间隔

    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 + 50):                 # 形成最后全屏弹窗,50 可任意改
        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.001)                       # 形成弹窗间隔时间

    time.sleep(3)                               # 生成后停留 3s

    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 一行就能换掉所有气泡文案;想让最后一句是别的,改 main() 里那行 tip = "我喜欢你"。

tips = ["今天辛苦了", "记得吃早饭", "我在等你", "只对你好"]

常见问题

  • 双击没反应 / 闪退:多半是没装 Python,或者装的是 Microsoft Store 版。命令行 python 爱心.py 看报错。
  • 杀毒软件报警:瞬间弹出几百个顶层窗口会被行为引擎当成恶意软件,加白名单即可。
  • 卡死:窗口太多,CPU 扛不住。把 200 调小、count+50 调小。
  • 看不到窗口:root.withdraw() 只隐藏主窗,Toplevel 会自动出现在最前;多屏时注意主屏设置。