简单的界面用于控制自动点击器
这个代码是一个基于Python的Tkinter GUI应用程序,它创建了一个简单的界面用于控制自动点击器。自动点击器可以设置点击间隔时间、鼠标按键类型(左键或右键)、点击方式(单击或双击)、重复次数和延迟时间,并支持添加和删除点击坐标。
首先,类`AutomaticClicker`继承了`tk.Tk`,表示这是一个Tkinter主窗口。初始化方法`__init__()`中设置了默认参数,并创建了各种控件,如文本标签、输入框、组合框、按钮等。这些控件通过`grid`布局管理器进行排列。
`create_widgets()`方法负责创建这些控件。例如,`interval_frame`是一个框架,包含了“每次鼠标点击的间隔时间”文本标签和一个输入框;`mouse_button_combobox`是一个只读的组合框,提供了选择鼠标左键或右键的功能;`click_type_combobox`是另一个只读的组合框,可以选择单击或双击;`repeat_times_entry`是一个输入框,用于输入重复次数;`delay_entry`也是一个输入框,用于输入延迟时间;`position_label`显示当前鼠标的坐标信息;`get_position_button`是一个按钮,点击后会获取当前鼠标的位置;`add_coordinate_button`用于添加当前鼠标位置到坐标列表;`use_current_position_checkbox`是一个勾选框,决定是否使用当前鼠标位置;`coords_treeview`是一个树视图,用来展示已添加的坐标及其相关参数;`delete_button`用于删除选中的行;`clear_button`用于清空整个列表;`start_button`和`stop_button`分别用于开始和停止自动点击。
`get_mouse_position()`方法用于获取当前鼠标的位置,并更新`position_label`的文本。
`add_current_position()`方法隐藏了窗口,然后定义了一个鼠标监听器`on_click`,当用户左键按下时,记录下鼠标的位置,再显示窗口并将这些信息添加到`coords_treeview`中。
`delete_selected_rows()`方法删除了树视图中选中的行。
`clear_list()`方法清空了树视图中的所有行。
`update_mouse_position()`方法创建了一个后台线程,每隔一段时间更新`position_label`的文本。
`start_clicking()`方法检查是否已经有点击任务在运行,如果有则返回。接着,它禁用开始按钮,启用停止按钮,并尝试从输入框中读取数据以设置点击参数。如果成功,它会根据用户的选择执行点击操作:如果勾选了“使用当前鼠标位置”,就调用`click_at_current_position()`方法;否则,循环遍历树视图中的每个坐标,调用`click_at_treeview_positions()`方法。最后,恢复开始和停止按钮的状态。
`click_at_current_position()`方法在一个新的线程里实现了对当前鼠标位置的点击。它会循环指定次数,打印出点击信息,并检查`is_running`标志位,如果不再运行则跳出循环。如果点击类型是单击,就调用`pyautogui.click()`函数;如果是双击,就调用`pyautogui.doubleClick()`函数。注意,这里的中文注释可能需要修改成英文或者去掉,因为Python代码通常使用英文注释。
`click_at_treeview_positions()`方法没有给出,应该是遍历树视图中的每一项,对每项的坐标进行点击操作。
总的来说,这个程序提供了一个简单易用的界面,让用户可以方便地配置自动点击器的各种参数,并能灵活地添加和删除点击坐标。
import tkinter as tk
from tkinter import ttk, messagebox
import pyautogui
import time
import threading
from pynput import mouseclass AutomaticClicker(tk.Tk):def __init__(self):super().__init__()self.title("自动点击器")self.geometry("600x600")self.is_running = Falseself.interval = 0.1 # 默认间隔时间0.1秒self.mouse_button = "left" # 默认左键点击self.click_type = "click" # 默认单击self.repeat_times = 999 # 默认重复次数self.delay = 0 # 默认延迟为0msself.use_current_position = tk.BooleanVar(value=False) # 是否使用当前鼠标位置self.create_widgets()self.update_mouse_position() # 开始实时更新鼠标位置def create_widgets(self):# 使用 grid 布局管理器self.grid_columnconfigure(0, weight=1)self.grid_rowconfigure(8, weight=1) # 使坐标列表框能够扩展# 每次鼠标点击的间隔时间self.interval_frame = ttk.Frame(self)self.interval_frame.grid(row=0, column=0, padx=(10, 0), pady=(10, 0), sticky=tk.W)self.interval_label = ttk.Label(self.interval_frame, text="每次鼠标点击的间隔时间(秒):")self.interval_label.pack(side=tk.LEFT)self.interval_entry = ttk.Entry(self.interval_frame, width=5)self.interval_entry.insert(0, str(self.interval)) # 默认值0.1秒self.interval_entry.pack(side=tk.LEFT)# 鼠标按键self.mouse_button_frame = ttk.Frame(self)self.mouse_button_frame.grid(row=1, column=0, padx=(10, 0), pady=(10, 0), sticky=tk.W)self.mouse_button_label = ttk.Label(self.mouse_button_frame, text="鼠标按键:")self.mouse_button_label.pack(side=tk.LEFT)self.mouse_button_combobox = ttk.Combobox(self.mouse_button_frame, values=["鼠标左键", "鼠标右键"], state="readonly")self.mouse_button_combobox.current(0) # 默认左键self.mouse_button_combobox.pack(side=tk.LEFT)# 点击方式self.click_type_frame = ttk.Frame(self)self.click_type_frame.grid(row=2, column=0, padx=(10, 0), pady=(10, 0), sticky=tk.W)self.click_type_label = ttk.Label(self.click_type_frame, text="点击方式:")self.click_type_label.pack(side=tk.LEFT)self.click_type_combobox = ttk.Combobox(self.click_type_frame, values=["鼠标单击", "鼠标双击"], state="readonly")self.click_type_combobox.current(0) # 默认单击self.click_type_combobox.pack(side=tk.LEFT)# 重复次数self.repeat_times_frame = ttk.Frame(self)self.repeat_times_frame.grid(row=3, column=0, padx=(10, 0), pady=(10, 0), sticky=tk.W)self.repeat_times_label = ttk.Label(self.repeat_times_frame, text="重复次数:")self.repeat_times_label.pack(side=tk.LEFT)self.repeat_times_entry = ttk.Entry(self.repeat_times_frame, width=5)self.repeat_times_entry.insert(0, str(self.repeat_times)) # 默认999次self.repeat_times_entry.pack(side=tk.LEFT)# 延迟self.delay_frame = ttk.Frame(self)self.delay_frame.grid(row=4, column=0, padx=(10, 0), pady=(10, 0), sticky=tk.W)self.delay_label = ttk.Label(self.delay_frame, text="延迟(ms):")self.delay_label.pack(side=tk.LEFT)self.delay_entry = ttk.Entry(self.delay_frame, width=5)self.delay_entry.insert(0, str(self.delay)) # 默认0msself.delay_entry.pack(side=tk.LEFT)# 显示当前鼠标位置self.position_label = ttk.Label(self, text="当前鼠标位置: ")self.position_label.grid(row=5, column=0, columnspan=2, padx=(10, 0), pady=(10, 0), sticky=tk.W)# 获取鼠标位置按钮self.get_position_button = ttk.Button(self, text="手动获取当前鼠标位置", command=self.get_mouse_position, width=20)self.get_position_button.grid(row=6, column=0, columnspan=2, padx=(10, 0), pady=(10, 0), sticky=tk.W)# 添加坐标按钮self.add_coordinate_button = ttk.Button(self, text="添加坐标", command=self.add_current_position, width=10)self.add_coordinate_button.grid(row=7, column=0, columnspan=2, padx=(10, 0), pady=(10, 0), sticky=tk.W)# 使用当前鼠标位置的复选框self.use_current_position_checkbox = ttk.Checkbutton(self,text="使用当前鼠标位置",variable=self.use_current_position,onvalue=True,offvalue=False,)self.use_current_position_checkbox.grid(row=8, column=0, padx=(10, 0), pady=(10, 0), sticky=tk.W)# 坐标列表self.coords_treeview = ttk.Treeview(self, columns=("X坐标", "Y坐标", "重复次数", "延迟(ms)"), show="headings")self.coords_treeview.column("#0", width=100, minwidth=0, stretch=True)self.coords_treeview.column("X坐标", width=100, minwidth=0, stretch=False)self.coords_treeview.column("Y坐标", width=100, minwidth=0, stretch=False)self.coords_treeview.column("重复次数", width=100, minwidth=0, stretch=False)self.coords_treeview.column("延迟(ms)", width=100, minwidth=0, stretch=False)self.coords_treeview.heading("#0", text="")self.coords_treeview.heading("X坐标", text="X坐标")self.coords_treeview.heading("Y坐标", text="Y坐标")self.coords_treeview.heading("重复次数", text="重复次数")self.coords_treeview.heading("延迟(ms)", text="延迟(ms)")self.coords_treeview.grid(row=9,column=0,columnspan=2,padx=(10, 0),pady=(10, 0),sticky=tk.W + tk.N + tk.S,)# 删除选中行按钮self.delete_button = ttk.Button(self, text="删除选中行", command=self.delete_selected_rows, width=10)self.delete_button.grid(row=10, column=0, padx=(10, 0), pady=(10, 0), sticky=tk.W)# 清空列表按钮self.clear_button = ttk.Button(self, text="清空列表", command=self.clear_list, width=10)self.clear_button.grid(row=10, column=1, padx=(10, 0), pady=(10, 0), sticky=tk.W)# 开始按钮self.start_button = ttk.Button(self, text="开始", command=self.start_clicking, width=10)self.start_button.grid(row=11, column=0, padx=(10, 0), pady=(10, 0), sticky=tk.W)# 停止按钮self.stop_button = ttk.Button(self, text="停止", command=self.stop_clicking, state=tk.DISABLED, width=10)self.stop_button.grid(row=11, column=1, padx=(10, 0), pady=(10, 0), sticky=tk.W)def get_mouse_position(self):x, y = pyautogui.position()self.position_label.config(text=f"当前鼠标位置: ({int(x)}, {int(y)})")def add_current_position(self):# 隐藏窗口self.withdraw()# 定义鼠标点击事件处理函数def on_click(x, y, button, pressed):nonlocal listenerif button == mouse.Button.left and pressed:# 停止监听并记录坐标listener.stop()self.x, self.y = x, y# 开始监听鼠标点击with mouse.Listener(on_click=on_click) as listener:listener.join()# 显示窗口self.deiconify()repeat_times = int(self.repeat_times_entry.get())delay = int(self.delay_entry.get())# 向 Treeview 中插入新行self.coords_treeview.insert("", tk.END, values=(self.x, self.y, repeat_times, delay))def delete_selected_rows(self):for item in self.coords_treeview.selection():self.coords_treeview.delete(item)def clear_list(self):for item in self.coords_treeview.get_children():self.coords_treeview.delete(item)def update_mouse_position(self):def update():while True:if not self.is_running: # 如果正在运行点击,则不更新位置x, y = pyautogui.position()self.position_label.config(text=f"当前鼠标位置: ({int(x)}, {int(y)})")time.sleep(0.2)# 使用线程来避免阻塞主线程threading.Thread(target=update, daemon=True).start()def start_clicking(self):if self.is_running:print("已经有一个点击任务在运行...")returnprint("开始点击...")self.is_running = Trueself.start_button.config(state=tk.DISABLED)self.stop_button.config(state=tk.NORMAL)try:self.interval = float(self.interval_entry.get())self.repeat_times = int(self.repeat_times_entry.get())self.delay = int(self.delay_entry.get())button_map = {"鼠标左键": "left", "鼠标右键": "right"}self.mouse_button = button_map[self.mouse_button_combobox.get()]click_map = {"鼠标单击": "click", "鼠标双击": "doubleClick"}self.click_type = click_map[self.click_type_combobox.get()]if self.use_current_position.get():# 对当前鼠标位置执行点击操作self.click_at_current_position()else:# 循环遍历Treeview中的每一个坐标self.click_at_treeview_positions()self.is_running = Falseself.start_button.config(state=tk.NORMAL)self.stop_button.config(state=tk.DISABLED)except ValueError:messagebox.showerror("错误", "请输入有效的数字!")self.is_running = Falseself.start_button.config(state=tk.NORMAL)self.stop_button.config(state=tk.DISABLED)def click_at_current_position(self):# 在独立线程中实现对当前鼠标位置的点击def click_thread():print("点击当前鼠标位置线程启动...")for _ in range(self.repeat_times):if not self.is_running:breakprint(f"正在点击... (is_running={self.is_running})")if self.click_type == "click":pyautogui.click(button=self.mouse_button, interval=self.interval)elif self.click_type == "doubleClick":pyautogui.doubleClick(button=self.mouse_button, interval=self.interval)time.sleep(self.delay / 1000.0) # 将毫秒转换为秒print("点击当前鼠标位置线程结束...")# 创建并启动线程clicking_thread = threading.Thread(target=click_thread, daemon=True)clicking_thread.start()def click_at_treeview_positions(self):# 在独立线程中实现对Treeview中坐标的点击def click_thread():print("点击Treeview位置线程启动...")for item in self.coords_treeview.get_children():if not self.is_running:breakx, y, repeat, delay = self.coords_treeview.item(item, "values")x, y, repeat, delay = int(x), int(y), int(repeat), int(delay)for _ in range(repeat):if not self.is_running:breakprint(f"正在点击... (is_running={self.is_running}, x={x}, y={y})")if self.click_type == "click":pyautogui.click(x=x, y=y, button=self.mouse_button, interval=self.interval)elif self.click_type == "双击":pyautogui.doubleClick(x=x, y=y, button=self.mouse_button, interval=self.interval)time.sleep(delay / 1000.0) # 将毫秒转换为秒print("点击Treeview位置线程结束...")# 创建并启动线程clicking_thread = threading.Thread(target=click_thread, daemon=True)clicking_thread.start()def stop_clicking(self):print("停止点击...")self.is_running = Falseself.start_button.config(state=tk.NORMAL)self.stop_button.config(state=tk.DISABLED)if __name__ == "__main__":app = AutomaticClicker()app.mainloop()