效果图:


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()
直接输入运行即可!

4万+

被折叠的 条评论
为什么被折叠?



