你看我的这一串代码行不行:?import tkinter as tk
import time
import threading
import random
import math
class PrankWithDancingMenApp:
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.taskbar_height = 40
# 状态变量
self.click_count = 0
self.crazy_mode = False
self.dancing_men = [] # 存储所有跳舞火柴人
self.selected_time = 0 # 用户选择的查看时长(秒)
# 先显示时间选择弹窗
self.create_time_select_window()
self.root.mainloop()
# ---------------------- 新增:时间选择弹窗 ----------------------
def create_time_select_window(self):
"""最开始的时间选择弹窗(无控制键,5个时长按钮)"""
self.time_win = tk.Toplevel(self.root)
self.time_win.overrideredirect(True) # 删除所有控制键
self.time_win.configure(bg="darkgray")
self.time_win.attributes("-topmost", True) # 始终置顶
# 窗口大小和居中
win_width, win_height = 500, 350
x_pos = (self.screen_width - win_width) // 2
y_pos = (self.screen_height - win_height) // 2
self.time_win.geometry(f"{win_width}x{win_height}+{x_pos}+{y_pos}")
# 标题文字
tk.Label(
self.time_win,
text="选择查看时间时长",
font=("微软雅黑", 18, "bold"),
bg="darkgray",
fg="white"
).pack(pady=20)
# 按钮框架(5个按钮分两行放,更美观)
btn_frame = tk.Frame(self.time_win, bg="darkgray")
btn_frame.pack(pady=10, padx=30)
# 第一行按钮:10秒、30秒、60秒
btn1 = tk.Button(
btn_frame,
text="查看10秒时间",
font=("微软雅黑", 12),
width=12,
height=2,
command=lambda: self.confirm_time(10)
)
btn1.grid(row=0, column=0, padx=10, pady=10)
btn2 = tk.Button(
btn_frame,
text="查看30秒时间",
font=("微软雅黑", 12),
width=12,
height=2,
command=lambda: self.confirm_time(30)
)
btn2.grid(row=0, column=1, padx=10, pady=10)
btn3 = tk.Button(
btn_frame,
text="查看60秒时间",
font=("微软雅黑", 12),
width=12,
height=2,
command=lambda: self.confirm_time(60)
)
btn3.grid(row=0, column=2, padx=10, pady=10)
# 第二行按钮:30分钟(1800秒)、1小时(3600秒)
btn4 = tk.Button(
btn_frame,
text="查看30分钟时间",
font=("微软雅黑", 12),
width=12,
height=2,
command=lambda: self.confirm_time(1800)
)
btn4.grid(row=1, column=0, padx=10, pady=10)
btn5 = tk.Button(
btn_frame,
text="查看1小时时间",
font=("微软雅黑", 12),
width=12,
height=2,
command=lambda: self.confirm_time(3600)
)
btn5.grid(row=1, column=1, columnspan=2, padx=10, pady=10) # 跨两列
def confirm_time(self, time_seconds):
"""用户选择时长后,关闭选择窗,进入点10下环节"""
self.selected_time = time_seconds # 记录选择的时长(实际后续没用,纯迷惑)
self.time_win.destroy() # 关闭时间选择窗
self.create_first_window() # 打开原来的“点10下”窗口
# ---------------------- 原有功能(保持不变) ----------------------
def create_first_window(self):
# 第一个窗口:点10下查看时间
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_width, win_height = 400, 200
x_pos = (self.screen_width - win_width) // 2
y_pos = (self.screen_height - win_height) // 2
self.first_win.geometry(f"{win_width}x{win_height}+{x_pos}+{y_pos}")
self.btn1 = tk.Button(
self.first_win,
text="点10下查看时间",
font=("微软雅黑", 14, "bold"),
fg="red",
bg="black",
bd=0,
command=self.increment_click
)
self.btn1.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.screen_width}x{self.screen_height}+0+0")
self.full_win.attributes("-topmost", True)
self.full_win.configure(bg="black")
self.time_label = tk.Label(
self.full_win,
text="",
font=("微软雅黑", 72, "bold"),
fg="red",
bg="black"
)
self.time_label.pack(expand=True)
self.update_time()
# 这里用用户选择的时长?不!还是固定10秒后弹确认窗(整蛊核心)
threading.Thread(target=self.delay_confirm_window, daemon=True).start()
def delay_confirm_window(self):
time.sleep(10) # 不管选多久,都是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
# 时间显示
self.confirm_time_label = tk.Label(
self.confirm_win,
text="",
font=("微软雅黑", 18, "bold"),
fg="red",
bg="white"
)
self.confirm_time_label.pack(pady=10)
self.update_confirm_time()
# 提示文字
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
)
self.confirm_btn.pack(side=tk.LEFT, padx=10)
self.cancel_btn = tk.Button(
btn_frame,
text="取消",
font=("微软雅黑", 12),
width=8,
fg="blue",
command=self.trick_cancel
)
self.cancel_btn.pack(side=tk.LEFT, padx=10)
# 初始位置
self.confirm_win.geometry(f"+{self.screen_width//2-200}+{self.screen_height//2-100}")
def update_confirm_time(self):
current_time = time.strftime("%Y-%m-%d %H:%M:%S")
self.confirm_time_label.config(text=current_time)
self.confirm_win.after(1000, self.update_confirm_time)
def move_window_insane(self):
if not self.crazy_mode or self.move_lock:
return
self.move_lock = True
win_width = 400
win_height = 200
safe_area = 0.1
min_x = int(self.screen_width * safe_area)
max_x = int(self.screen_width * (1 - safe_area) - win_width)
min_y = int(self.screen_height * safe_area)
max_y = int((self.screen_height - self.taskbar_height) * (1 - safe_area) - win_height)
new_x = random.randint(min_x, max_x)
new_y = random.randint(min_y, max_y)
self.confirm_win.geometry(f"+{new_x}+{new_y}")
self.confirm_win.after(80, lambda: setattr(self, "move_lock", False))
self.confirm_win.after(80, self.move_window_insane)
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.9)
size = random.randint(60, 100)
man_win.geometry(f"{size}x{size}")
# 随机位置
x = random.randint(50, self.screen_width - size - 50)
y = random.randint(50, self.screen_height - self.taskbar_height - size - 50)
man_win.geometry(f"+{x}+{y}")
# 创建画布
canvas = tk.Canvas(man_win, width=size, height=size, bg=self.root["bg"], highlightthickness=0)
canvas.pack()
# 随机颜色和动作(1=扭腰 2=挥手跳 3=转圈)
colors = ["red", "green", "blue", "purple", "orange", "pink", "yellow"]
color = random.choice(colors)
action = random.randint(1, 3)
# 存储火柴人状态
man_data = {
"window": man_win,
"canvas": canvas,
"size": size,
"color": color,
"angle": 0,
"spin": 0,
"direction": 1,
"action": action
}
self.dancing_men.append(man_data)
# 开始动画
self.animate_dancing_man(man_data)
# 存在时间3-5秒
man_win.after(random.randint(3000, 5000), lambda: self.remove_dancing_man(man_data))
def animate_dancing_man(self, man_data):
"""3种动作动画"""
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")
# 基础参数
body_height = size * 0.6
head_radius = size * 0.15
limb_length = size * 0.25
center_x = size // 2
center_y = size * 0.8
# 动作1:扭腰摆胯
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"]
body_top_x = center_x + math.sin(angle) * (body_height * 0.3)
body_top_y = center_y - body_height + math.cos(angle) * (body_height * 0.1)
canvas.create_line(center_x, center_y, body_top_x, body_top_y, width=3, fill=color)
head_x = body_top_x + math.sin(angle * 1.2) * head_radius
head_y = body_top_y
canvas.create_oval(head_x-head_radius, head_y-head_radius,
head_x+head_radius, head_y+head_radius, fill=color)
arm_angle = angle * 1.8
left_arm_x = body_top_x - math.sin(arm_angle) * limb_length
left_arm_y = body_top_y + math.cos(arm_angle) * limb_length
right_arm_x = body_top_x + math.sin(arm_angle) * limb_length
right_arm_y = body_top_y + math.cos(arm_angle) * limb_length
canvas.create_line(body_top_x, body_top_y, left_arm_x, left_arm_y, width=3, fill=color)
canvas.create_line(body_top_x, body_top_y, right_arm_x, right_arm_y, width=3, fill=color)
leg_angle = -angle * 1.5
left_leg_x = center_x - math.sin(leg_angle) * limb_length
left_leg_y = center_y + math.cos(leg_angle) * limb_length
right_leg_x = center_x + math.sin(leg_angle) * limb_length
right_leg_y = center_y + math.cos(leg_angle) * limb_length
canvas.create_line(center_x, center_y, left_leg_x, left_leg_y, width=3, fill=color)
canvas.create_line(center_x, center_y, right_leg_x, right_leg_y, width=3, fill=color)
# 动作2:挥手跳
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_offset = math.sin(angle) * (body_height * 0.15)
body_top_x = center_x
body_top_y = center_y - body_height - jump_offset
canvas.create_line(center_x, center_y - jump_offset, body_top_x, body_top_y, width=3, fill=color)
canvas.create_oval(body_top_x-head_radius, body_top_y-head_radius,
body_top_x+head_radius, body_top_y+head_radius, fill=color)
arm_angle = math.sin(angle * 3) * 1.5
left_arm_x = body_top_x - math.cos(arm_angle) * limb_length
left_arm_y = body_top_y + math.sin(arm_angle) * limb_length
right_arm_x = body_top_x + math.cos(arm_angle) * limb_length
right_arm_y = body_top_y + math.sin(arm_angle) * limb_length
canvas.create_line(body_top_x, body_top_y, left_arm_x, left_arm_y, width=3, fill=color)
canvas.create_line(body_top_x, body_top_y, right_arm_x, right_arm_y, width=3, fill=color)
leg_angle = abs(angle)
left_leg_x = center_x - math.sin(leg_angle) * (limb_length * 0.7)
left_leg_y = (center_y - jump_offset) + math.cos(leg_angle) * (limb_length * 0.7)
right_leg_x = center_x + math.sin(leg_angle) * (limb_length * 0.7)
right_leg_y = (center_y - jump_offset) + math.cos(leg_angle) * (limb_length * 0.7)
canvas.create