这篇文章上次修改于 222 天前,可能其部分内容已经发生变化,如有疑问可询问作者。

这是一个用于舒缓眼睛的小程序,活动活动眼睛。

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
import tkinter as tk
import random

class EyeRelaxation:
def __init__(self, master):
self.master = master
master.title("眼睛放松")
master.geometry("1920x1080") # 设置分辨率
master.configure(bg="#6E7B6C") # 设置窗口背景颜色

self.canvas = tk.Canvas(master, width=1920, height=1080, bg="#6E7B6C", highlightthickness=0)
self.canvas.pack(fill=tk.BOTH, expand=1)

self.ball_size = 40 # 小球大小
self.ball = self.canvas.create_oval(10, 10, self.ball_size+10, self.ball_size+10, fill="#C7EDCC", outline="#C7EDCC")
self.ball_visible = True # 记录小球是否可见

self.move_ball()
master.bind("<space>", self.toggle_ball) # 绑定空格键

def move_ball(self):
if self.ball_visible:
try:
x1, y1, x2, y2 = self.canvas.coords(self.ball)
ball_center_x = (x1 + x2) / 2
ball_center_y = (y1 + y2) / 2

new_x = random.randint(0, 1920)
new_y = random.randint(0, 1080)

self.canvas.move(self.ball, new_x - ball_center_x, new_y - ball_center_y)
except tk.TclError:
pass # 避免异常

self.master.after(2000, self.move_ball) # 保持移动频率

def toggle_ball(self, event):
if self.ball_visible:
self.canvas.itemconfigure(self.ball, state='hidden')
else:
self.canvas.itemconfigure(self.ball, state='normal')
self.ball_visible = not self.ball_visible

if __name__ == "__main__":
root = tk.Tk()
app = EyeRelaxation(root)
root.mainloop()

保存为XX.py

这是个python程序,需要下载python.
安装完成后,用cmd可以运行。

使用愉快~

(更新:2025年3月14日)