wei/测试/弹窗.py

109 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import tkinter as tk
import random
# 弹窗计数器(初始为0)
window_count = 0
# 存储所有弹窗的列表
windows_list = []
# 最大弹窗数量
MAX_WINDOWS = 3
def create_warm_tip():
global window_count # 声明使用全局计数器
# 创建弹窗(关联主窗口 root)
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)
# 弹窗数量+1
window_count += 1
def auto_pop_tips(interval=200): # 间隔时间(毫秒)0.3秒=300毫秒
# 只有当弹窗数量小于300时才继续创建
if window_count < MAX_WINDOWS:
create_warm_tip() # 创建一个弹窗
# 继续定时递归调用(实现循环弹窗)
root.after(interval, auto_pop_tips, interval)
else:
# 达到300个弹窗后打印提示并开始关闭弹窗
print(f"已达到最大弹窗数量({MAX_WINDOWS}个),开始逐个关闭")
# 延迟1秒后开始逐个关闭弹窗
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() # 隐藏主窗口
# 启动定时弹窗(间隔0.3秒)
auto_pop_tips(200)
# 启动主循环
root.mainloop()