python代码之彩虹便利贴
一、代码
import tkinter as tk
import randomclass TipApp:def __init__(self):self.batch_windows = [] # 存储批量弹窗的列表self.single_window = None # 单个初始弹窗# 定义暖心话语列表self.tips = ['多喝水哦~', '保持微笑呀', '元气满满~', '吃点水果呀', '心情要好~','好好爱自己~', '我想你啦~', '梦想会实现~', '期待见面呀~', '金榜题名哦~','一切顺利~', '早点休息呀', '烦恼拜拜~', '别熬夜啦~', '今天开心吗~','天冷加衣~', '晚安呀~', '吃早餐啦~', '午休一会儿~', '发呆也可以~','你超棒的~', '下雨带伞~', '换季添衣~', '试试别怕~', '坚持住呀~','偷懒没事~', '都会好的~', '出去走走~', '好好吃饭~', '笑起来美~','抬头看天~', '累了歇歇~', '加油呀~', '别着急~', '今天很棒~','多散步呀~', '记得开心~', '你值得~', '慢慢来~', '阳光很好~','风很温柔~', '花开啦~', '月亮圆~', '星星亮~', '好好睡觉~','别逞强呀~', '有人惦记~', '会幸福的~', '平安喜乐~', '温暖常在~']# 定义背景颜色列表self.bg_colors = ['lightpink', 'skyblue', 'lightgreen', 'lavender','lightyellow', 'plum', 'coral', 'bisque', 'aquamarine']def show_single_tip(self):"""显示单个初始弹窗,关闭后启动批量弹窗"""self.single_window = tk.Tk()self.single_window.title('温馨提示')# 窗口居中显示screen_width = self.single_window.winfo_screenwidth()screen_height = self.single_window.winfo_screenheight()window_width = 280window_height = 100x = (screen_width - window_width) // 2y = (screen_height - window_height) // 2self.single_window.geometry(f'{window_width}x{window_height}+{x}+{y}')# 初始提示语tip = '点击关闭将开启批量提示~'bg = 'skyblue'tk.Label(self.single_window,text=tip,bg=bg,font=('微软雅黑', 14),width=30,height=3).pack()# 绑定空格键关闭self.single_window.bind('<space>', self.on_space_global)# 窗口置顶self.single_window.attributes('-topmost', True)# 关闭单个弹窗后启动批量弹窗self.single_window.protocol("WM_DELETE_WINDOW", self.start_batch_tips)self.single_window.mainloop()def create_batch_window(self, count):"""创建批量弹窗,递归调用生成多个窗口"""if count <= 0:return# 创建子窗口(避免多个主窗口冲突)window = tk.Toplevel()self.batch_windows.append(window) # 加入列表便于管理# 随机位置(确保窗口在屏幕内)screen_width = window.winfo_screenwidth()screen_height = window.winfo_screenheight()window_width = 280window_height = 100x = random.randrange(start=0, stop=screen_width - window_width)y = random.randrange(start=0, stop=screen_height - window_height)window.geometry(f'{window_width}x{window_height}+{x}+{y}')# 随机选择暖心话语和背景色tip = random.choice(self.tips)bg = random.choice(self.bg_colors)# 添加标签显示文字tk.Label(window,text=tip,bg=bg,font=('微软雅黑', 14),width=30,height=3).pack()# 绑定空格键关闭window.bind('<space>', self.on_space_global)# 窗口置顶window.attributes('-topmost', True)# 10秒后自动消失window.after(10000, window.destroy)# 递归创建下一个窗口(间隔50毫秒,避免瞬间卡顿)if count > 1:window.after(50, self.create_batch_window, count - 1)def start_batch_tips(self):"""关闭单个弹窗后,启动批量弹窗生成"""# 先销毁单个弹窗if self.single_window:self.single_window.destroy()self.single_window = None# 创建新的根窗口来管理批量弹窗root = tk.Tk()root.withdraw() # 隐藏主窗口# 使用after方法延迟创建批量弹窗root.after(100, lambda: self.create_batch_window(50)) # 减少到50个窗口,避免过多root.mainloop()def on_space_global(self, event=None):"""全局空格键处理:关闭所有窗口并退出程序"""# 关闭单个弹窗if self.single_window:try:self.single_window.destroy()except:passself.single_window = None# 关闭所有批量弹窗for window in self.batch_windows:try:window.destroy()except:passself.batch_windows = []# 退出程序import syssys.exit()if __name__ == '__main__':app = TipApp()app.show_single_tip() # 先显示单个初始弹窗二、代码打包
1.安装pyinstaller打包工具
pip install pyinstaller2. 打包你的程序
pyinstaller --onefile --windowed --name="彩虹便利贴" your_script_name.py--onefile:打包成单个可执行文件--windowed:不显示命令行窗口(适合 GUI 程序)--name:设置生成的可执行文件名称打包完成后,在项目目录下的
dist文件夹中找到 彩虹便利贴.exe文件。
三、添加图标
pyinstaller --onefile --windowed --icon=your_icon.ico --name="暖心提示" your_script_name.py


