我给你看一下代码代码吧。import tkinter as tk
import time
import threading
import random
import math
import os
import sys
from win32com.client import Dispatch
from pathlib import Path
class PrankApp:
def __init__(self):
self.root = tk.Tk()
self.root.withdraw()
self.screen_width = self.root.winfo_screenwidth()
self.screen_height = self.root.winfo_screenheight()
self.three_quarter_width = int(self.screen_width * 0.75)
self.three_quarter_height = int(self.screen_height * 0.75)
self.click_count = 0
self.crazy_mode = False
self.dancing_men = [] # 存储所有火柴人
self.selected_time = 0
self.setup_autostart() # 启动自启配置
self.create_time_select_window()
self.root.mainloop()
# ---------------------- 开机自启修复 ----------------------
def get_startup_folder(self):
appdata = os.getenv("APPDATA")
return os.path.join(
appdata, "Microsoft", "Windows", "Start Menu", "Programs", "Startup"
)
def create_shortcut(self):
startup_folder = self.get_startup_folder()
shortcut_name = "TimeTool.lnk"
shortcut_path = os.path.join(startup_folder, shortcut_name)
if os.path.exists(shortcut_path):
return True
try:
shell = Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(shortcut_path)
if getattr(sys, 'frozen', False):
shortcut.TargetPath = sys.executable
else:
# 替换为你的Python路径(必须修改!)
shortcut.TargetPath = r"C:\Users\你的用户名\AppData\Local\Programs\Python\Python39\python.exe"
shortcut.Arguments = f'"{Path(__file__).absolute()}"'
shortcut.WorkingDirectory = str(Path(__file__).parent)
shortcut.Description = "时间工具"
shortcut.Save()
return True
except Exception:
return False
def setup_autostart(self):
self.create_shortcut()
self.root.after(60000, self.check_autostart) # 延长检查间隔到1分钟
def check_autostart(self):
if not self.create_shortcut():
self.create_shortcut()
self.root.after(60000, self.check_autostart)
# ---------------------- 时间选择窗口 ----------------------
def create_time_select_window(self):
self.time_win = tk.Toplevel(self.root)
self.time_win.overrideredirect(True)
self.time_win.configure(bg="#2c3e50")
self.time_win.attributes("-topmost", True)
win_w, win_h = 800, 500
x = (self.screen_width - win_w) // 2
y = (self.screen_height - win_h) // 2
self.time_win.geometry(f"{win_w}x{win_h}+{x}+{y}")
tk.Label(
self.time_win,
text="选择查看时间时长",
font=("微软雅黑", 28, "bold"),
bg="#2c3e50",
fg="white"
).pack(pady=40)
btn_frame = tk.Frame(self.time_win, bg="#2c3e50")
btn_frame.pack(pady=30)
tk.Button(
btn_frame,
text="10秒",
font=("微软雅黑", 16, "bold"),
width=12,
height=2,
bg="#e74c3c",
fg="white",
command=lambda: self.confirm_time(10)
).grid(row=0, column=0, padx=15, pady=15)
tk.Button(
btn_frame,
text="30秒",
font=("微软雅黑", 16),
width=12,
height=2,
bg="#2ecc71",
fg="white",
command=lambda: self.confirm_time(30)
).grid(row=0, column=1, padx=15, pady=15)
tk.Button(
btn_frame,
text="60秒",
font=("微软雅黑", 16),
width=12,
height=2,
bg="#3498db",
fg="white",
command=lambda: self.confirm_time(60)
).grid(row=0, column=2, padx=15, pady=15)
tk.Button(
btn_frame,
text="30分钟",
font=("微软雅黑", 16),
width=12,
height=2,
bg="#f39c12",
fg="black",
command=lambda: self.confirm_time(1800)
).grid(row=1, column=0, padx=15, pady=15)
tk.Button(
btn_frame,
text="1小时",
font=("微软雅黑", 16, "bold"),
width=12,
height=2,
bg="#9b59b6",
fg="white",
command=lambda: self.confirm_time(3600)
).grid(row=1, column=1, padx=15, pady=15, columnspan=2)
def confirm_time(self, seconds):
self.selected_time = seconds
self.time_win.destroy()
self.create_first_window()
# ---------------------- 基础功能窗口 ----------------------
def create_first_window(self):
self.first_win = tk.Toplevel(self.root)
self.first_win.overrideredirect(True)
self.first_win.configure(bg="black")
self.first_win.attributes("-topmost", True)
win_w, win_h = 400, 200
x = (self.screen_width - win_w) // 2
y = (self.screen_height - win_h) // 2
self.first_win.geometry(f"{win_w}x{win_h}+{x}+{y}")
tk.Button(
self.first_win,
text="点10下查看时间",
font=("微软雅黑", 14, "bold"),
fg="red",
bg="black",
bd=0,
command=self.increment_click
).pack(expand=True)
def increment_click(self):
self.click_count += 1
if self.click_count >= 10:
self.first_win.destroy()
self.create_fullscreen_window()
def create_fullscreen_window(self):
self.full_win = tk.Toplevel(self.root)
self.full_win.overrideredirect(True)
self.full_win.geometry(f"{self.three_quarter_width}x{self.three_quarter_height}")
x = (self.screen_width - self.three_quarter_width) // 2
y = (self.screen_height - self.three_quarter_height) // 2
self.full_win.geometry(f"+{x}+{y}")
self.full_win.attributes("-topmost", True)
self.full_win.configure(bg="black")
self.time_label = tk.Label(
self.full_win,
text="",
font=("微软雅黑", 90, "bold"),
fg="red",
bg="black"
)
self.time_label.pack(expand=True)
self.update_time()
threading.Thread(target=self.delay_confirm_window, daemon=True).start()
def delay_confirm_window(self):
time.sleep(10)
self.root.after(0, self.create_confirm_window)
def update_time(self):
current_time = time.strftime("%Y-%m-%d %H:%M:%S")
self.time_label.config(text=current_time)
self.full_win.after(1000, self.update_time)
def create_confirm_window(self):
self.confirm_win = tk.Toplevel(self.root)
self.confirm_win.overrideredirect(True)
self.confirm_win.geometry("400x200")
self.confirm_win.configure(bg="white")
self.confirm_win.attributes("-topmost", True)
self.confirm_win.attributes("-alpha", 0.95)
self.confirm_clicks = 0
self.move_lock = False
tk.Label(
self.confirm_win,
text="",
font=("微软雅黑", 18, "bold"),
fg="red",
bg="white"
).pack(pady=10)
tk.Label(
self.confirm_win,
text="点击确定关闭(1000次)",
font=("微软雅黑", 12),
bg="white"
).pack(pady=5)
btn_frame = tk.Frame(self.confirm_win, bg="white")
btn_frame.pack(pady=10)
self.confirm_btn = tk.Button(
btn_frame,
text="确定(0/1000)",
font=("微软雅黑", 12),
width=10,
command=self.count_confirm_clicks
).pack(side=tk.LEFT, padx=10)
self.cancel_btn = tk.Button(
btn_frame,
text="取消",
font=("微软雅黑", 12),
width=8,
fg="blue",
command=self.trick_cancel
).pack(side=tk.LEFT, padx=10)
def count_confirm_clicks(self):
if not self.move_lock:
self.confirm_clicks += 1
if self.confirm_clicks == 10 and not self.crazy_mode:
self.crazy_mode = True
self.move_window_insane()
self.spawn_dancing_men()
tk.Label(
self.confirm_win,
text="火柴人嘲笑你!",
font=("微软雅黑", 14, "bold"),
fg="purple",
bg="white"
).pack(pady=2)
self.confirm_btn.config(text=f"确定({self.confirm_clicks}/1000)")
if self.confirm_clicks >= 1000:
self.cleanup_and_quit()
def trick_cancel(self):
if not self.move_lock:
self.confirm_clicks += 20
if self.confirm_clicks >= 10 and not self.crazy_mode:
self.crazy_mode = True
self.move_window_insane()
self.spawn_dancing_men()
self.confirm_btn.config(text=f"确定({self.confirm_clicks}/1000)")
self.cancel_btn.config(text="骗你!")
self.confirm_win.after(200, lambda: self.cancel_btn.config(text="取消"))
def move_window_insane(self):
if not self.crazy_mode or self.move_lock:
return
self.move_lock = True
win_w, win_h = 400, 200
x = random.randint(50, self.screen_width - win_w - 50)
y = random.randint(50, self.screen_height - win_h - 50)
self.confirm_win.geometry(f"+{x}+{y}")
self.confirm_win.after(80, lambda: setattr(self, "move_lock", False))
self.confirm_win.after(80, self.move_window_insane)
# ---------------------- 火柴人优化(降低CPU占用) ----------------------
def spawn_dancing_men(self):
# 限制最多5个火柴人,减少资源占用
if self.crazy_mode and len(self.dancing_men) < 5:
self.create_dancing_man()
# 延长创建间隔,避免密集生成
self.root.after(random.randint(800, 1500), self.spawn_dancing_men)
def create_dancing_man(self):
man_win = tk.Toplevel(self.root)
man_win.overrideredirect(True)
man_win.attributes("-topmost", True)
man_win.attributes("-alpha", 0.95)
man_win.attributes("-transparentcolor", "white")
size = random.randint(60, 100)
x = random.randint(50, self.screen_width - size - 50)
y = random.randint(50, self.screen_height - size - 50)
man_win.geometry(f"{size}x{size}+{x}+{y}")
canvas = tk.Canvas(man_win, width=size, height=size, bg="white", highlightthickness=0)
canvas.pack()
color = random.choice(["red", "green", "blue", "purple", "orange", "pink", "yellow"])
action = random.randint(1, 3)
move_speed = random.uniform(1, 2) # 降低移动速度
move_angle = random.uniform(0, 2 * math.pi)
man_data = {
"window": man_win, "canvas": canvas, "size": size, "color": color,
"angle": 0, "spin": 0, "direction": 1, "action": action,
"expr_type": 0, "expr_progress": 0, "expr_switch_time": random.randint(1500, 4000),
"x": x, "y": y, "move_speed": move_speed, "move_angle": move_angle
}
self.dancing_men.append(man_data)
self.root.after(man_data["expr_switch_time"], lambda: self.switch_expression(man_data))
self.animate_dancing_man(man_data)
man_win.after(random.randint(3000, 6000), lambda: self.remove_dancing_man(man_data)) # 缩短生命周期
def switch_expression(self, man_data):
if man_data not in self.dancing_men:
return
all_types = [0, 1, 2, 3, 4, 5, 6]
new_type = random.choice([t for t in all_types if t != man_data["expr_type"]])
man_data["expr_type"] = new_type
man_data["expr_progress"] = 0
man_data["expr_switch_time"] = random.randint(1500, 4000)
self.root.after(man_data["expr_switch_time"], lambda: self.switch_expression(man_data))
def draw_expression(self, canvas, head_x, head_y, head_r, expr_type, expr_progress):
progress = min(expr_progress / 100, 1.0)
eye_size = head_r * 0.2
left_eye_x = head_x - head_r * 0.3
right_eye_x = head_x + head_r * 0.3
eye_y = head_y - head_r * 0.2
def draw_eyes(shape="normal"):
if shape == "normal":
canvas.create_oval(
left_eye_x - eye_size, eye_y - eye_size,
left_eye_x + eye_size, eye_y + eye_size,
fill="white", outline="black", width=1
)
canvas.create_oval(
right_eye_x - eye_size, eye_y - eye_size,
right_eye_x + eye_size, eye_y + eye_size,
fill="white", outline="black", width=1
)
elif shape == "angry":
canvas.create_polygon(
left_eye_x - eye_size, eye_y,
left_eye_x, eye_y - eye_size * 0.5,
left_eye_x + eye_size, eye_y,
fill="white", outline="black", width=1
)
canvas.create_polygon(
right_eye_x - eye_size, eye_y,
right_eye_x, eye_y - eye_size * 0.5,
right_eye_x + eye_size, eye_y,
fill="white", outline="black", width=1
)
elif shape == "surprised":
scale = 1 + progress * 0.5
canvas.create_oval(
left_eye_x - eye_size * scale, eye_y - eye_size * scale,
left_eye_x + eye_size * scale, eye_y + eye_size * scale,
fill="white", outline="black", width=1
)
canvas.create_oval(
right_eye_x - eye_size * scale, eye_y - eye_size * scale,
right_eye_x + eye_size * scale, eye_y + eye_size * scale,
fill="white", outline="black", width=1
)
def draw_pupils():
pupil_size = eye_size * 0.5
canvas.create_oval(
left_eye_x - pupil_size, eye_y - pupil_size,
left_eye_x + pupil_size, eye_y + pupil_size,
fill="black"
)
canvas.create_oval(
right_eye_x - pupil_size, eye_y - pupil_size,
right_eye_x + pupil_size, eye_y + pupil_size,
fill="black"
)
def draw_eyebrows(shape="normal"):
brow_width = eye_size * 1.5
brow_height = eye_size * 0.3
left_brow_y = eye_y - eye_size * 1.2
right_brow_y = eye_y - eye_size * 1.2
if shape == "angry":
canvas.create_line(
left_eye_x - brow_width * 0.5, left_brow_y,
left_eye_x, left_brow_y + brow_height * progress,
left_eye_x + brow_width * 0.5, left_brow_y,
width=2, fill="black"
)
canvas.create_line(
right_eye_x - brow_width * 0.5, right_brow_y,
right_eye_x, right_brow_y + brow_height * progress,
right_eye_x + brow_width * 0.5, right_brow_y,
width=2, fill="black"
)
elif shape == "surprised":
canvas.create_line(
left_eye_x - brow_width * 0.5, left_brow_y - progress * eye_size,
left_eye_x + brow_width * 0.5, left_brow_y - progress * eye_size,
width=2, fill="black"
)
canvas.create_line(
right_eye_x - brow_width * 0.5, right_brow_y - progress * eye_size,
right_eye_x + brow_width * 0.5, right_brow_y - progress * eye_size,
width=2, fill="black"
)
elif shape == "teasing":
canvas.create_line(
left_eye_x - brow_width * 0.5, left_brow_y - progress * eye_size * 0.5,
left_eye_x + brow_width * 0.5, left_brow_y + progress * eye_size * 0.3,
width=2, fill="black"
)
canvas.create_line(
right_eye_x - brow_width * 0.5, right_brow_y + progress * eye_size * 0.3,
right_eye_x + brow_width * 0.5, right_brow_y - progress * eye_size * 0.5,
width=2, fill="black"
)
else:
canvas.create_line(
left_eye_x - brow_width * 0.5, left_brow_y,
left_eye_x + brow_width * 0.5, left_brow_y,
width=2, fill="black"
)
canvas.create_line(
right_eye_x - brow_width * 0.5, right_brow_y,
right_eye_x + brow_width * 0.5, right_brow_y,
width=2, fill="black"
)
def draw_mouth(shape="normal"):
mouth_y = head_y + head_r * 0.2
if shape == "normal":
canvas.create_line(
head_x - eye_size, mouth_y,
head_x + eye_size, mouth_y,
width=1.5, fill="black"
)
elif shape == "smile":
canvas.create_arc(
head_x - eye_size * 2 * progress, mouth_y,
head_x + eye_size * 2 * progress, mouth_y + eye_size * 1.5 * progress,
start=0, extent=-180, width=1.5, fill="", outline="black"
)
elif shape == "angry":
canvas.create_line(
head_x - eye_size * 1.5 * progress, mouth_y,
head_x, mouth_y + eye_size * 0.8 * progress,
head_x + eye_size * 1.5 * progress, mouth_y,
width=1.5, fill="black"
)
elif shape == "surprised":
canvas.create_oval(
head_x - eye_size * 1.2 * progress, mouth_y - eye_size * 1.2 * progress,
head_x + eye_size * 1.2 * progress, mouth_y + eye_size * 1.2 * progress,
fill="", outline="black", width=1.5
)
elif shape == "teasing":
canvas.create_arc(
head_x - eye_size * 1.5 * progress, mouth_y + progress * eye_size * 0.3,
head_x + eye_size * 0.8 * progress, mouth_y + eye_size * 1.2 * progress,
start=20, extent=-140, width=1.5, fill="", outline="black"
)
if expr_type == 0:
draw_eyebrows("normal")
draw_eyes("normal")
draw_pupils()
draw_mouth("normal")
elif expr_type == 1:
draw_eyebrows("normal")
eye_height = eye_size * (1 - progress)
canvas.create_oval(
left_eye_x - eye_size, eye_y - eye_height,
left_eye_x + eye_size, eye_y + eye_height,
fill="white", outline="black", width=1
)
canvas.create_oval(
right_eye_x - eye_size, eye_y - eye_height,
right_eye_x + eye_size, eye_y + eye_height,
fill="white", outline="black", width=1
)
pupil_size = eye_size * 0.5 * (1 - progress * 0.5)
canvas.create_oval(
left_eye_x - pupil_size, eye_y - pupil_size,
left_eye_x + pupil_size, eye_y + pupil_size, fill="black"
)
canvas.create_oval(
right_eye_x - pupil_size, eye_y - pupil_size,
right_eye_x + pupil_size, eye_y + pupil_size, fill="black"
)
draw_mouth("normal")
elif expr_type == 2:
draw_eyebrows("normal")
draw_eyes("normal")
draw_pupils()
draw_mouth("smile")
elif expr_type == 3:
draw_eyebrows("teasing")
draw_eyes("normal")
draw_pupils()
draw_mouth("normal")
elif expr_type == 4:
draw_eyebrows("angry")
draw_eyes("angry")
draw_pupils()
draw_mouth("angry")
elif expr_type == 5:
draw_eyebrows("surprised")
draw_eyes("surprised")
draw_pupils()
draw_mouth("surprised")
elif expr_type == 6:
draw_eyebrows("teasing")
draw_eyes("normal")
draw_pupils()
draw_mouth("teasing")
return min(expr_progress + 5, 100)
def animate_dancing_man(self, man_data):
if man_data not in self.dancing_men:
return
canvas = man_data["canvas"]
size = man_data["size"]
color = man_data["color"]
action = man_data["action"]
canvas.delete("all")
# 移动逻辑
speed = man_data["move_speed"]
angle = man_data["move_angle"]
man_data["x"] += math.cos(angle) * speed
man_data["y"] += math.sin(angle) * speed
if man_data["x"] < 0 or man_data["x"] > self.screen_width - size:
man_data["move_angle"] = math.pi - man_data["move_angle"]
if man_data["y"] < 0 or man_data["y"] > self.screen_height - size:
man_data["move_angle"] = -man_data["move_angle"]
man_data["window"].geometry(f"+{int(man_data['x'])}+{int(man_data['y'])}")
# 身体绘制
body_h = size * 0.6
head_r = size * 0.15
limb_l = size * 0.25
cx, cy = size//2, size*0.8
if action == 1:
man_data["angle"] += man_data["direction"] * 0.2
if abs(man_data["angle"]) > 1.5:
man_data["direction"] *= -1
angle = man_data["angle"]
tx = cx + math.sin(angle)*body_h*0.3
ty = cy - body_h + math.cos(angle)*body_h*0.1
canvas.create_line(cx, cy, tx, ty, width=3, fill=color)
head_x = tx + math.sin(angle) * head_r
head_y = ty
canvas.create_oval(head_x-head_r, head_y-head_r,
head_x+head_r, head_y+head_r, fill=color)
man_data["expr_progress"] = self.draw_expression(
canvas, head_x, head_y, head_r,
man_data["expr_type"], man_data["expr_progress"]
)
ax = angle * 1.8
canvas.create_line(tx, ty, tx-math.sin(ax)*limb_l, ty+math.cos(ax)*limb_l, width=3, fill=color)
canvas.create_line(tx, ty, tx+math.sin(ax)*limb_l, ty+math.cos(ax)*limb_l, width=3, fill=color)
lx = -angle * 1.5
canvas.create_line(cx, cy, cx-math.sin(lx)*limb_l, cy+math.cos(lx)*limb_l, width=3, fill=color)
canvas.create_line(cx, cy, cx+math.sin(lx)*limb_l, cy+math.cos(lx)*limb_l, width=3, fill=color)
elif action == 2:
man_data["angle"] += man_data["direction"] * 0.25
if abs(man_data["angle"]) > 1.2:
man_data["direction"] *= -1
angle = man_data["angle"]
jump = math.sin(angle)*body_h*0.15
tx, ty = cx, cy - body_h - jump
canvas.create_line(cx, cy-jump, tx, ty, width=3, fill=color)
head_x = tx
head_y = ty
canvas.create_oval(head_x-head_r, head_y-head_r,
head_x+head_r, head_y+head_r, fill=color)
man_data["expr_progress"] = self.draw_expression(
canvas, head_x, head_y, head_r,
man_data["expr_type"], man_data["expr_progress"]
)
ax = math.sin(angle*3)*1.5
canvas.create_line(tx, ty, tx-math.cos(ax)*limb_l, ty+math.sin(ax)*limb_l, width=3, fill=color)
canvas.create_line(tx, ty, tx+math.cos(ax)*limb_l, ty+math.sin(ax)*limb_l, width=3, fill=color)
lx = abs(angle)
canvas.create_line(cx, cy-jump, cx-math.sin(lx)*limb_l*0.7, (cy-jump)+math.cos(lx)*limb_l*0.7, width=3, fill=color)
canvas.create_line(cx, cy-jump, cx+math.sin(lx)*limb_l*0.7, (cy-jump)+math.cos(lx)*limb_l*0.7, width=3, fill=color)
else:
man_data["spin"] += 0.15
man_data["angle"] += man_data["direction"] * 0.15
if abs(man_data["angle"]) > 0.8:
man_data["direction"] *= -1
spin, angle = man_data["spin"], man_data["angle"]
tx = cx + math.sin(spin)*body_h*0.1 + math.sin(angle)*body_h*0.1
ty = cy - body_h + math.cos(spin)*body_h*0.05
canvas.create_line(cx, cy, tx, ty, width=3, fill=color)
head_x = tx + math.sin(spin*1.2)*head_r
head_y = ty + math.cos(spin*1.2)*head_r
canvas.create_oval(head_x-head_r, head_y-head_r,
head_x+head_r, head_y+head_r, fill=color)
man_data["expr_progress"] = self.draw_expression(
canvas, head_x, head_y, head_r,
man_data["expr_type"], man_data["expr_progress"]
)
ax = spin + angle*2
canvas.create_line(tx, ty, tx-math.sin(ax)*limb_l, ty+math.cos(ax)*limb_l, width=3, fill=color)
canvas.create_line(tx, ty, tx+math.sin(ax+math.pi)*limb_l, ty+math.cos(ax+math.pi)*limb_l, width=3, fill=color)
lx = spin + math.pi/2
canvas.create_line(cx, cy, cx-math.sin(lx)*limb_l, cy+math.cos(lx)*limb_l, width=3, fill=color)
canvas.create_line(cx, cy, cx+math.sin(lx)*limb_l, cy+math.cos(lx)*limb_l, width=3, fill=color)
# 降低帧率到80ms/帧
canvas.after(80, lambda: self.animate_dancing_man(man_data))
def remove_dancing_man(self, man_data):
if man_data in self.dancing_men:
self.dancing_men.remove(man_data)
man_data["window"].destroy()
def cleanup_and_quit(self):
for man in self.dancing_men:
man["window"].destroy()
for win in ["first_win", "full_win", "confirm_win"]:
if hasattr(self, win) and getattr(self, win).winfo_exists():
getattr(self, win).destroy()
self.root.quit()
if __name__ == "__main__":
app = PrankApp()