python满屏字.py
· 2.8 KiB · Python
Eredeti
import tkinter as tk
import random
import threading
import time
def show_warn_tip():
# 创建窗口
window = tk.Tk()
# 获取屏幕宽高
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
# 窗口尺寸扩大1.5倍
window_width = 270 # 180 * 1.5 = 270
window_height = 75 # 50 * 1.5 = 75
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}")
# 关心生活健康的话语(30条)
tips = [
'好好吃饭', '好好休息', '早点休息', '天天开心',
'记得喝水', '按时吃饭', '别熬夜了', '照顾好自己',
'注意身体', '记得运动', '放松一下', '保持微笑',
'劳逸结合', '别太劳累', '记得午休', '多吃水果',
'出去走走', '呼吸新鲜空气', '保持好心情', '别久坐',
'记得早餐', '保护眼睛', '注意保暖', '别着凉',
'保持健康', '平安喜乐', '开心每一天', '一切顺利',
'万事如意', '心想事成'
]
tip = random.choice(tips)
# 更多背景颜色选择
bg_colors = [
'lightpink', 'skyblue', 'lightgreen', 'lavender', 'lightyellow',
'plum', 'coral', 'bisque', 'aquamarine', 'mistyrose', 'honeydew',
'peachpuff', 'paleturquoise', 'lavenderblush', 'oldlace', 'lemonchiffon',
'lightcyan', 'lightgray', 'lightpink', 'lightsalmon', 'lightseagreen',
'lightskyblue', 'lightslategray', 'lightsteelblue', 'lightyellow'
]
bg = random.choice(bg_colors)
# 先创建并显示Label,再设置窗口属性
label = tk.Label(
window,
text=tip,
bg=bg,
font=('仿宋', 18),
width=15,
height=2
)
label.pack()
# 立即更新窗口显示
window.update()
# 窗口置顶
window.attributes('-topmost', True)
# 3秒自动关闭(加快关闭速度,避免长期占用资源)
window.after(10000, window.destroy)
window.mainloop()
if __name__ == "__main__":
# 根据屏幕大小计算所需窗口数量(确保铺满)
window_count = 300 # 减少窗口数量,避免过于密集
# 快速创建窗口(缩短间隔)
for i in range(window_count):
t = threading.Thread(target=show_warn_tip)
t.daemon = True # 守护线程,主程序退出时自动结束
t.start()
time.sleep(0.01) # 极短间隔,快速创建
# 保持主程序运行
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
# 按Ctrl+C退出
pass
| 1 | import tkinter as tk |
| 2 | import random |
| 3 | import threading |
| 4 | import time |
| 5 | |
| 6 | def show_warn_tip(): |
| 7 | # 创建窗口 |
| 8 | window = tk.Tk() |
| 9 | |
| 10 | # 获取屏幕宽高 |
| 11 | screen_width = window.winfo_screenwidth() |
| 12 | screen_height = window.winfo_screenheight() |
| 13 | |
| 14 | # 窗口尺寸扩大1.5倍 |
| 15 | window_width = 270 # 180 * 1.5 = 270 |
| 16 | window_height = 75 # 50 * 1.5 = 75 |
| 17 | x = random.randrange(0, screen_width - window_width) |
| 18 | y = random.randrange(0, screen_height - window_height) |
| 19 | |
| 20 | # 设置窗口标题和位置 |
| 21 | window.title('提示') |
| 22 | window.geometry(f"{window_width}x{window_height}+{x}+{y}") |
| 23 | |
| 24 | # 关心生活健康的话语(30条) |
| 25 | tips = [ |
| 26 | '好好吃饭', '好好休息', '早点休息', '天天开心', |
| 27 | '记得喝水', '按时吃饭', '别熬夜了', '照顾好自己', |
| 28 | '注意身体', '记得运动', '放松一下', '保持微笑', |
| 29 | '劳逸结合', '别太劳累', '记得午休', '多吃水果', |
| 30 | '出去走走', '呼吸新鲜空气', '保持好心情', '别久坐', |
| 31 | '记得早餐', '保护眼睛', '注意保暖', '别着凉', |
| 32 | '保持健康', '平安喜乐', '开心每一天', '一切顺利', |
| 33 | '万事如意', '心想事成' |
| 34 | ] |
| 35 | |
| 36 | tip = random.choice(tips) |
| 37 | |
| 38 | # 更多背景颜色选择 |
| 39 | bg_colors = [ |
| 40 | 'lightpink', 'skyblue', 'lightgreen', 'lavender', 'lightyellow', |
| 41 | 'plum', 'coral', 'bisque', 'aquamarine', 'mistyrose', 'honeydew', |
| 42 | 'peachpuff', 'paleturquoise', 'lavenderblush', 'oldlace', 'lemonchiffon', |
| 43 | 'lightcyan', 'lightgray', 'lightpink', 'lightsalmon', 'lightseagreen', |
| 44 | 'lightskyblue', 'lightslategray', 'lightsteelblue', 'lightyellow' |
| 45 | ] |
| 46 | bg = random.choice(bg_colors) |
| 47 | |
| 48 | # 先创建并显示Label,再设置窗口属性 |
| 49 | label = tk.Label( |
| 50 | window, |
| 51 | text=tip, |
| 52 | bg=bg, |
| 53 | font=('仿宋', 18), |
| 54 | width=15, |
| 55 | height=2 |
| 56 | ) |
| 57 | label.pack() |
| 58 | |
| 59 | # 立即更新窗口显示 |
| 60 | window.update() |
| 61 | |
| 62 | # 窗口置顶 |
| 63 | window.attributes('-topmost', True) |
| 64 | |
| 65 | # 3秒自动关闭(加快关闭速度,避免长期占用资源) |
| 66 | window.after(10000, window.destroy) |
| 67 | |
| 68 | window.mainloop() |
| 69 | |
| 70 | if __name__ == "__main__": |
| 71 | # 根据屏幕大小计算所需窗口数量(确保铺满) |
| 72 | window_count = 300 # 减少窗口数量,避免过于密集 |
| 73 | |
| 74 | # 快速创建窗口(缩短间隔) |
| 75 | for i in range(window_count): |
| 76 | t = threading.Thread(target=show_warn_tip) |
| 77 | t.daemon = True # 守护线程,主程序退出时自动结束 |
| 78 | t.start() |
| 79 | time.sleep(0.01) # 极短间隔,快速创建 |
| 80 | |
| 81 | # 保持主程序运行 |
| 82 | try: |
| 83 | while True: |
| 84 | time.sleep(1) |
| 85 | except KeyboardInterrupt: |
| 86 | # 按Ctrl+C退出 |
| 87 | pass |