这个项目是一个鼠标操作录制和回放工具,它有以下作用和意义:
自动化重复任务:通过录制和回放鼠标操作,可以自动化一些重复性的任务,比如网页测试、游戏操作等。
提高效率:对于需要频繁进行相同鼠标操作的用户,使用这个工具可以显著提高工作效率。
学习和研究:对于研究用户行为或进行人机交互研究的用户,这个工具可以作为研究工具,帮助他们分析和理解用户的鼠标操作习惯。
辅助功能:对于有特殊需求的用户,比如需要模拟鼠标操作进行辅助功能测试,这个工具可以提供帮助。
import os
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import pyautogui
import time
from pynput import mouse
class MouseRecorder:
def __init__(self, root):
self.root = root
self.root.title("鼠标操作录制与执行")
self.recording = False
self.operations = []
self.script_file = None
# UI组件
self.start_button = tk.Button(root, text="开始录制", command=self.start_recording)
self.start_button.pack()
self.stop_button = tk.Button(root, text="结束录制", command=self.stop_recording, state="disabled")
self.stop_button.pack()
self.play_button = tk.Button(root, text="启动执行", command=self.start_playing, state="disabled")
self.play_button.pack()
self.stop_program_button = tk.Button(root, text="停止程序", command=self.stop_program)
self.stop_program_button.pack()
self.select_script_button = tk.Button(root, text="选择脚本", command=self.select_script)
self.select_script_button.pack()
self.times_label = tk.Label(root, text="重复执行次数:")
self.times_label.pack()
self.times_entry = tk.Entry(root)
self.times_entry.pack()
self.interval_label = tk.Label(root, text="执行间隔时间(秒):")
self.interval_label.pack()
self.interval_entry = tk.Entry(root)
self.interval_entry.pack()
def start_recording(self):
self.recording = True
self.operations = []
self.start_button.config(state="disabled")
self.stop_button.config(state="normal")
self.play_button.config(state="normal")
messagebox.showinfo("提示", "开始录制鼠标操作")
# 定义鼠标事件监听函数
def on_move(x, y):
if self.recording:
self.operations.append(f"move_to {x} {y}")
def on_click(x, y, button, pressed):
if self.recording and pressed: # Only record when the button is pressed
action = "left" if button == mouse.Button.left else "right"
self.operations.append(f"{action}_click {x} {y}")
# 监听鼠标事件
self.listener = mouse.Listener(on_move=on_move, on_click=on_click)
self.listener.start()
def stop_recording(self):
# 停止监听鼠标事件
if hasattr(self, 'listener'):
self.listener.stop()
self.listener.join()
self.recording = False
# 检查是否有操作被记录
if not self.operations:
messagebox.showerror("错误", "没有记录到任何操作")
return
self.start_button.config(state="normal")
self.stop_button.config(state="disabled")
pyautogui.mousemove = None
pyautogui.mouseclick = None
self.script_file = f"mouse_script_{time.strftime('%Y%m%d%H%M%S')}.txt"
with open(self.script_file, "w") as f:
for op in self.operations:
f.write(op + "\n")
messagebox.showinfo("提示", "录制结束,脚本已保存")
# 打印脚本文件路径,用于调试
print(f"Script file created: {self.script_file}")
def start_playing(self):
try:
times = int(self.times_entry.get())
interval = float(self.interval_entry.get())
except ValueError:
messagebox.showerror("错误", "请输入有效的数字")
return
self.script_file = filedialog.askopenfilename(filetypes=[("Text files", "*.txt")])
if not self.script_file: # 确保文件路径不是空的
messagebox.showerror("错误", "未选择文件")
return
# 确保文件存在且不为空
if not os.path.isfile(self.script_file) or os.path.getsize(self.script_file) == 0:
messagebox.showerror("错误", "选择的文件不存在或为空")
return
with open(self.script_file, "r") as f:
operations = f.read().splitlines()
# 打印操作列表,用于调试
print(f"Operations to play: {operations}")
def play_operations():
print(operations)
for _ in range(times):
for op in operations:
command, *args = op.split()
if command == "move_to":
pyautogui.moveTo(int(args[0]), int(args[1]))
elif command == "click":
pyautogui.click()
elif command == "right_click":
pyautogui.rightClick()
time.sleep(interval)
self.play_button.config(state="disabled")
self.stop_program_button.config(state="normal")
# messagebox.showinfo("提示", "开始执行鼠标操作")
play_operations()
def stop_program(self):
messagebox.showinfo("提示", "程序已停止")
self.root.destroy()
def select_script(self):
self.script_file = filedialog.askopenfilename(filetypes=[("Text files", "*.txt")])
if self.script_file:
self.times_entry.delete(0, tk.END)
self.times_entry.insert(0, "1")
self.interval_entry.delete(0, tk.END)
self.interval_entry.insert(0, "1")
self.play_button.config(state="normal")
if __name__ == "__main__":
root = tk.Tk()
app = MouseRecorder(root)
root.mainloop()