Python實(shí)現(xiàn)愛(ài)心彈窗的完整教學(xué)(附源碼)
一段不到 40 行的 Python 代碼,在屏幕上依次綻放出愛(ài)心形狀的彈窗,每一條都是你想對(duì) TA 說(shuō)的話。
效果預(yù)覽

程序運(yùn)行后會(huì)出現(xiàn)三個(gè)階段:
- 愛(ài)心綻放 — 100 個(gè)小彈窗沿心形曲線依次彈出,每 0.03 秒一個(gè),最終拼成一顆完整的愛(ài)心。
- 滿屏暴擊 — 愛(ài)心消失后,彈窗鋪滿整個(gè)屏幕,每個(gè)窗口都是一句暖心的叮囑。
- 溫柔收?qǐng)?/strong> — 等待 10 秒后,所有彈窗在 1 秒內(nèi)優(yōu)雅地逐個(gè)關(guān)閉。
按下 空格鍵 可以隨時(shí)退出。
核心原理
1. 心形曲線公式
愛(ài)心形狀來(lái)自經(jīng)典的參數(shù)方程:
- x=16sin3(t)
- y=13cos(t)−5cos(2t)−2cos(3t)−cos(4t)
將參數(shù) t 從 0 到 2π 均勻取 100 個(gè)點(diǎn),就能得到一個(gè)平滑的心形輪廓。然后將其縮放并居中到屏幕中央。
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. 彈窗窗口
每個(gè)彈窗本質(zhì)上是一個(gè) 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) # 隨機(jī)選取一句暖心話
color = random.choice(colors) # 隨機(jī)背景色
tk.Label(win, text=text, bg=color,
font=("微軟雅黑", 14), width=20, height=3).pack()
return win
-topmost:確保彈窗始終在最前方。- 隨機(jī)文案:每次從預(yù)設(shè)列表中隨機(jī)抽取一條溫馨提醒。
- 隨機(jī)配色:粉色、淺藍(lán)、淺綠等柔和色調(diào),視覺(jué)上溫馨可愛(ài)。
3. 三階段動(dòng)畫流程
# 階段一:愛(ài)心綻放
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)
# 銷毀愛(ài)心彈窗
# 階段二:滿屏暴擊
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)
# 階段三:優(yōu)雅關(guān)閉
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 = ["多喝水利", "好好愛(ài)自己", "好好吃飯", "保持好心情",
"我想你了", "順順利利", "別熬夜", "天涼了多穿衣服"]
colors = ["pink", "lightblue", "lightgreen", "lemonchiffon",
"hotpink", "skyblue"]
def heart_points(n, screen_w, screen_h):
"""生成心形曲線上的 n 個(gè)屏幕坐標(biāo)點(diǎ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):
"""在指定位置創(chuàng)建一個(gè)彈窗"""
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()
# ---- 階段一:愛(ài)心綻放 ----
points = heart_points(100, sw, sh)
for i, (x, y) in enumerate(points):
tip = "充實(shí)自己" 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)
# ---- 階段三:優(yōu)雅關(guān)閉 ----
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 認(rèn)可的顏色名或十六進(jìn)制值:
colors = ["#FFB6C1", "#87CEEB", "#DDA0DD", "#98FB98"]
調(diào)整愛(ài)心大小
修改 heart_points 函數(shù)中 x * 20 和 y * 20 的縮放倍數(shù),數(shù)字越大愛(ài)心越大。
調(diào)整彈窗數(shù)量
- 愛(ài)心彈窗數(shù):修改
heart_points(100, ...)中的100 - 滿屏彈窗數(shù):修改
sw // 150 * sh // 40 + 50中的公式
運(yùn)行方式
確保已安裝 Python 3(自帶 tkinter),直接運(yùn)行:
python 愛(ài)心彈窗.py
僅依賴 Python 標(biāo)準(zhǔn)庫(kù),無(wú)需安裝任何第三方包。
小結(jié)
這個(gè)小程序用到的知識(shí)點(diǎn):
| 知識(shí)點(diǎn) | 說(shuō)明 |
|---|---|
| tkinter | Python 標(biāo)準(zhǔn) GUI 庫(kù) |
| 心形參數(shù)方程 | 數(shù)學(xué)之美 |
| time.sleep + update() | 手動(dòng)實(shí)現(xiàn)逐幀動(dòng)畫 |
| random.choice | 隨機(jī)選取文案和顏色 |
| -topmost 屬性 | 窗口始終置頂 |
不到 40 行代碼,就能給 TA 一個(gè)小小的驚喜??烊ピ囋嚢?!
以上就是Python實(shí)現(xiàn)愛(ài)心彈窗的完整教學(xué)(附源碼)的詳細(xì)內(nèi)容,更多關(guān)于Python愛(ài)心彈窗的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python中第三方庫(kù)Requests庫(kù)的高級(jí)用法詳解
雖然Python的標(biāo)準(zhǔn)庫(kù)中urllib2模塊已經(jīng)包含了平常我們使用的大多數(shù)功能,但是它的API使用起來(lái)讓人實(shí)在感覺(jué)不好。它已經(jīng)不適合現(xiàn)在的時(shí)代,不適合現(xiàn)代的互聯(lián)網(wǎng)了。而Requests的誕生讓我們有了更好的選擇。本文就介紹了Python中第三方庫(kù)Requests庫(kù)的高級(jí)用法。2017-03-03
Python2實(shí)現(xiàn)的圖片文本識(shí)別功能詳解
這篇文章主要介紹了Python2實(shí)現(xiàn)的圖片文本識(shí)別功能,結(jié)合實(shí)例形式分析了Python pytesser庫(kù)的安裝及使用pytesser庫(kù)識(shí)別圖片文字相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
python redis 批量設(shè)置過(guò)期key過(guò)程解析
這篇文章主要介紹了python redis 批量設(shè)置過(guò)期key過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Python大數(shù)據(jù)之從網(wǎng)頁(yè)上爬取數(shù)據(jù)的方法詳解
這篇文章主要介紹了Python大數(shù)據(jù)之從網(wǎng)頁(yè)上爬取數(shù)據(jù)的方法,結(jié)合實(shí)例形式詳細(xì)分析了Python爬蟲爬取網(wǎng)頁(yè)數(shù)據(jù)的相關(guān)操作技巧,需要的朋友可以參考下2019-11-11

