1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| import tkinter as tk import random
window_count = 0
windows_list = []
MAX_WINDOWS = 300
def create_warm_tip(): global window_count window = tk.Toplevel(root)
screen_width = root.winfo_screenwidth() screen_height = root.winfo_screenheight()
window_width = 250 window_height = 60 x = random.randrange(0, screen_width - window_width) y = random.randrange(0, screen_height - window_height)
window.title('温馨提示') window.geometry(f"{window_width}x{window_height}+{x}+{y}")
tips = [ '多喝水哦~', '保持微笑呀', '每天都要元气满满', '记得吃水果', '保持好心情', '好好爱自己', '我想你了', '梦想成真', '期待下一次见面', '金榜题名', '顺顺利利', '早点休息', '愿所有烦恼都消失', '别熬夜', '今天过得开心嘛', '天冷了,多穿衣服' ] tip = random.choice(tips)
bg_colors = [ 'lightpink', 'skyblue', 'lightgreen', 'lavender', 'lightyellow', 'plum', 'coral', 'bisque', 'aquamarine', 'mistyrose', 'honeydew', 'lavenderblush', 'oldlace' ] bg = random.choice(bg_colors)
tk.Label( window, text=tip, bg=bg, font=('微软雅黑', 16), width=30, height=3 ).pack()
window.attributes('-topmost', True)
windows_list.append(window) window_count += 1
def auto_pop_tips(interval=200): if window_count < MAX_WINDOWS: create_warm_tip() root.after(interval, auto_pop_tips, interval) else: print(f"已达到最大弹窗数量({MAX_WINDOWS}个),开始逐个关闭") root.after(1000, close_windows_one_by_one)
def close_windows_one_by_one(interval=100): if windows_list: window = windows_list.pop() window.destroy() print(f"剩余弹窗数量: {len(windows_list)}")
if windows_list: root.after(interval, close_windows_one_by_one, interval) else: print("所有弹窗已关闭") root.after(1000, root.destroy) else: print("所有弹窗已关闭") root.after(1000, root.destroy)
root = tk.Tk() root.withdraw()
auto_pop_tips(200)
root.mainloop()
|