开发过程
先说结论,这类脚本的核心不复杂:用 Tkinter 起多个窗口,再让它们持续出现在屏幕上。真正麻烦的不是'怎么弹',而是窗口数量一多之后,系统负担、关闭方式和异常处理都会开始变得很现实。
准备工作
先确认本机已经装好 Python。Tkinter 是 Python 标准库里的 GUI 工具包,做一些简单窗口程序够用了,不需要额外折腾第三方依赖。
编写代码
下面这个示例脚本会持续创建随机位置、随机大小、随机颜色的弹窗,每个窗口里显示一段告白文字。
import tkinter as tk
import random as ra
import threading as td
import time as ti
import sys
# 定义爱心文字列表,增加多样性
love_texts = [
'我喜欢你', '❤️', '喜欢你', 'love you', '想你了', '❤️❤️❤️', '永远爱你', '你最可爱', '心动瞬间', '满屏爱意', '❤️心动', '超喜欢你', '你是我的唯一', '我爱你', '❤️永恒', '我想和你在一起', '遇见你真好', '❤️心之所向', '每天想你', '你最特别', '一生有你', '❤️甜甜蜜蜜', '喜欢你的一切', '只喜欢你', '心跳加速', '❤️浪漫时光', '我愿意', '有你真好', '满眼都是你', '❤️不离不弃', '深情不及久伴', '爱你到永远', '你偷走了我的心', '❤️命中注定', , , , , , , , , ,
]
colors = [
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
]
fonts = [
(, ),
(, ),
(, ),
(, ),
(, )
]
():
:
root = tk.Tk()
width = ra.randint(, )
height = ra.randint(, )
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
x = ra.randint(, screenwidth - width)
y = ra.randint(, screenheight - height)
root.title(ra.choice([, , , ]))
root.geometry()
root.attributes(, ra.uniform(, ))
bg_color = ra.choice(colors)
fg_color =
text = ra.choice(love_texts)
font = ra.choice(fonts)
label = tk.Label(
root, text=text, fg=fg_color, bg=bg_color, font=font, width=width//, height=height//
)
label.pack(fill=tk.BOTH, expand=)
root.attributes(, )
root.protocol(, : root.destroy())
root.mainloop()
Exception e:
():
:
:
thread = td.Thread(target=create_popup)
thread.daemon =
thread.start()
ti.sleep()
KeyboardInterrupt:
()
sys.exit()
__name__ == :
:
()
()
popup_loop()
KeyboardInterrupt:
()

