-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
172 lines (132 loc) · 4.96 KB
/
Copy pathmain.py
File metadata and controls
172 lines (132 loc) · 4.96 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import tkinter as tk
import math
import random
import time
import queue
# 全局设置
WINDOW_W, WINDOW_H = 250, 100 # 单个小窗口的宽度和高度
DESIRED_POINTS = 300 # 爱心上的点数
NUM_LOOPS = 2 # 爱心圈数 - 可以自定义修改
# 创建队列用于线程间通信
window_queue = queue.Queue()
shutdown_event = False
windows = [] # 存储所有窗口
def generate_heart_points(num_points, num_loops, screen_w, screen_h, window_w, window_h):
"""生成多圈爱心形状的坐标点"""
points = []
center_x = screen_w // 2
center_y = screen_h // 2
# 总角度范围 = 圈数 * 2π
total_angle = num_loops * 2 * math.pi
for i in range(num_points):
# 心脏线参数方程 - 使用总角度范围
t = i / num_points * total_angle
x = 16 * (math.pow(math.sin(t), 3))
y = -(13 * math.cos(t) - 5 * math.cos(2 * t) - 2 * math.cos(3 * t) - math.cos(4 * t))
# 缩放以适应屏幕
scale = min(screen_w // 40, screen_h // 40)
x = center_x + x * scale
y = center_y + y * scale
# 确保窗口不超出屏幕边界
x = max(window_w // 2, min(x, screen_w - window_w // 2))
y = max(window_h // 2, min(y, screen_h - window_h // 2))
points.append((int(x), int(y)))
return points
def create_window(x, y, w, h, index):
"""创建单个提示窗口"""
root = tk.Toplevel() # 使用Toplevel而不是Tk
root.overrideredirect(False) # 显示边框
root.geometry(f"{w}x{h}+{x - w // 2}+{y - h // 2}")
root.attributes('-topmost', True) # 窗口置顶
# 随机背景色(浅色系)
r = random.randint(200, 255)
g = random.randint(200, 255)
b = random.randint(200, 255)
bg_color = f'#{r:02x}{g:02x}{b:02x}'
root.configure(bg=bg_color)
# 提示语列表
tips = [
"保持好心情", "我想你了", "保持微笑", "天天都要元气满满", "别熬夜",
"记得吃水果", "好好吃饭", "多喝水", "每天都要开心", "保持你的纯真",
"你是最棒的", "今天也要加油", "相信自己", "一切都会好的", "慢慢来",
"深呼吸", "放松一下", "你很特别", "世界因你美好", "保持可爱"
]
tip = random.choice(tips)
label = tk.Label(
root,
text=tip,
font=('微软雅黑', 10),
background=bg_color,
foreground='#333333' # 深灰色文字
)
label.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
# 更新窗口标题显示序号和圈数信息
loop_num = (index // (num_points // NUM_LOOPS)) + 1 if NUM_LOOPS > 1 else 1
root.title(f"爱心窗口 {index + 1} (第{loop_num}圈)")
# 将窗口添加到全局列表
windows.append(root)
return root
def process_queue():
"""处理窗口创建队列"""
try:
while True:
# 非阻塞方式获取队列中的任务
task = window_queue.get_nowait()
if task is None: # 停止信号
return
x, y, index = task
create_window(x, y, WINDOW_W, WINDOW_H, index)
except queue.Empty:
pass
# 如果没有停止信号,继续检查队列
if not shutdown_event:
root.after(100, process_queue)
def close_all_windows():
"""关闭所有窗口"""
global shutdown_event
shutdown_event = True
# 关闭所有窗口
for window in windows:
try:
window.destroy()
except:
pass
# 关闭主窗口
root.destroy()
def main():
global root, shutdown_event, num_points
# 创建主窗口但不显示
root = tk.Tk()
root.withdraw() # 隐藏主窗口
# 获取屏幕尺寸
SCREEN_W = root.winfo_screenwidth()
SCREEN_H = root.winfo_screenheight()
# 生成爱心基本点
base_points = generate_heart_points(DESIRED_POINTS, NUM_LOOPS, SCREEN_W, SCREEN_H, WINDOW_W, WINDOW_H)
num_points = len(base_points)
try:
# 启动队列处理
root.after(10, process_queue)
print(f"开始创建{NUM_LOOPS}圈爱心,共{num_points}个窗口...")
# 依次沿着爱心路径创建窗口
for i, (x, y) in enumerate(base_points):
if shutdown_event:
break
# 计算当前在第几圈
current_loop = (i // (num_points // NUM_LOOPS)) + 1 if NUM_LOOPS > 1 else 1
print(f"创建爱心窗口 {i + 1}/{num_points} (第{current_loop}圈) 位置: ({x}, {y})")
# 将窗口创建任务放入队列
window_queue.put((x, y, i))
# 控制创建速度,让窗口逐个出现
time.sleep(0.1)
# 处理Tkinter事件
root.update()
# 所有窗口创建完成后停留3秒
print(f"{NUM_LOOPS}圈爱心创建完成,停留3秒...")
time.sleep(3)
finally:
# 设置关闭信号释放所有窗口
print("释放所有窗口...")
close_all_windows()
if __name__ == "__main__":
main()