视频:
import tkinter as tk
import random
WINDOW_W, WINDOW_H = 300, 100 # 单个窗口尺寸
COUNT = 400 # 弹窗数量
SPAWN_INTERVAL_MS = 30 # 平均弹出间隔(越小越快)
AUTO_CLOSE_MS = 9000 # 自动关闭时间(毫秒)
tips = [
"多喝水呀~", "保持微笑呀~", "今天要开心哦~",
"别熬夜啦~", "休息一下吧~", "好好爱自己~",
"我想你了~", "加油呀~", "一切都会好起来的~",
"天冷了,多穿衣服~"
]
colors = [
'lightpink', 'skyblue', 'lightblue', 'lavender', 'honeydew',
'bisque', 'mistyrose', 'aquamarine', 'oldlace', 'palegreen'
]
windows = []
def spawn_one(root, screen_w, screen_h):
"""随机位置弹出一个窗口"""
win = tk.Toplevel(root)
windows.append(win)
# 随机位置(允许重叠)
x = random.randint(0, max(0, screen_w - WINDOW_W))
y = random.randint(0, max(0, screen_h - WINDOW_H))
# 随机外观
win.geometry(f"{WINDOW_W}x{WINDOW_H}+{x}+{y}")
win.attributes("-topmost", True)
win.title("温馨提示")
tk.Label(
win,
text=random.choice(tips),
bg=random.choice(colors),
font=("微软雅黑", 16),
width=100,
height=20
).pack()
def random_spawn_loop(root, screen_w, screen_h, remaining):
if remaining <= 0:
return
spawn_one(root, screen_w, screen_h)
# 每次间隔随机变化
delay = random.randint(SPAWN_INTERVAL_MS // 2, SPAWN_INTERVAL_MS * 2)
root.after(delay, random_spawn_loop, root, screen_w, screen_h, remaining - 1)
def close_all(root):
"""关闭所有窗口"""
for w in windows:
try:
w.destroy()
except:
pass
try:
root.destroy()
except:
pass
def bind_space_to_close(root):
root.bind("<space>", lambda e: close_all(root))
if __name__ == "__main__":
root = tk.Tk()
root.withdraw()
bind_space_to_close(root)
sw, sh = root.winfo_screenwidth(), root.winfo_screenheight()
# 启动随机弹窗循环
random_spawn_loop(root, sw, sh, COUNT)
# 自动关闭
root.after(AUTO_CLOSE_MS, lambda: close_all(root))
root.mainloop()
爱心
import tkinter as tk
import random
import math
WINDOW_W, WINDOW_H = 250, 60
COUNT = 100
BASE_DELAY_MS = 15
TIME_JITTER_MS = 10
POS_JITTER_PX = 12
AUTO_CLOSE_MS = 12000
tips = [
"多喝水呀~", "保持微笑呀~", "每天都要元气满满~",
"记得吃早饭~", "好好爱自己~", "我想你了~",
"顺顺利利~", "早上好呀~", "今天过得开心吗~", "天冷了,多穿衣服~"
]
colors = [
'lightpink', 'skyblue', 'lightblue', 'lavender', 'honeydew',
'bisque', 'mistyrose', 'aquamarine', 'oldlace'
]
windows = []
def heart_xy(t: float):
"""爱心参数方程"""
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)
return (x, y)
def compute_heart_points(screen_w, screen_h, n):
"""生成逆时针爱心路径,从顶部(t=0)开始"""
ts = [0 + (2 * math.pi) * i / (n - 1) for i in range(n)] # 从 t=0 开始,逆时针走 0→2π
pts = [heart_xy(t) for t in ts]
# 坐标范围
xs, ys = [p[0] for p in pts], [p[1] for p in pts]
min_x, max_x = min(xs), max(xs)
min_y, max_y = min(ys), max(ys)
span_x, span_y = max_x - min_x, max_y - min_y
margin = 40
usable_w = max(200, screen_w - WINDOW_W - 2 * margin)
usable_h = max(200, screen_h - WINDOW_H - 2 * margin)
scale = min(usable_w / span_x, usable_h / span_y)
cx, cy = screen_w // 2, screen_h // 2
mapped = []
for x, y in pts:
sx = int(cx + (x - (min_x + span_x / 2)) * scale)
sy = int(cy - (y - (min_y + span_y / 2)) * scale)
sx -= WINDOW_W // 2
sy -= WINDOW_H // 2
sx += random.randint(-POS_JITTER_PX, POS_JITTER_PX)
sy += random.randint(-POS_JITTER_PX, POS_JITTER_PX)
sx = max(0, min(screen_w - WINDOW_W, sx))
sy = max(0, min(screen_h - WINDOW_H, sy))
mapped.append((sx, sy))
return mapped # 不需要再排序了,我们直接按 t 顺序生成
def spawn_one(root, pos):
win = tk.Toplevel(root)
windows.append(win)
x, y = pos
win.geometry(f"{WINDOW_W}x{WINDOW_H}+{x}+{y}")
win.attributes("-topmost", True)
win.title("温馨提示")
tk.Label(
win,
text=random.choice(tips),
bg=random.choice(colors),
font=("微软雅黑", 16),
width=30, height=3
).pack()
def spawn_heart_sequence(root, positions, idx=0):
if idx >= len(positions):
return
spawn_one(root, positions[idx])
delay = max(1, BASE_DELAY_MS + random.randint(-TIME_JITTER_MS, TIME_JITTER_MS))
root.after(delay, spawn_heart_sequence, root, positions, idx + 1)
def close_all(root):
for w in windows:
try: w.destroy()
except: pass
try: root.destroy()
except: pass
def bind_space_to_close(root):
root.bind("<space>", lambda e: close_all(root))
if __name__ == "__main__":
root = tk.Tk()
root.withdraw()
bind_space_to_close(root)
sw, sh = root.winfo_screenwidth(), root.winfo_screenheight()
positions = compute_heart_points(sw, sh, COUNT)
# 依次弹出(从上尖开始逆时针)
spawn_heart_sequence(root, positions)
# 10 秒后自动关闭
root.after(AUTO_CLOSE_MS, lambda: close_all(root))
root.mainloop()
太棒了
indonesia vacation packages Logan E. The ATV sunset tour was incredible — great photos and great vibes. https://linktr.ee/travelshopbooking
Keno’s probability is fascinating – so much relies on random draws! It’s cool to see games like free solitaire offer a different kind of mental challenge, instantly accessible without downloads – a nice break from number crunching!