You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
Web3Tools/test/click_test.py

133 lines
4.8 KiB

import os
import pyautogui
import tkinter as tk
from tkinter import messagebox
import time
class AutoClicker:
def __init__(self, src_folder="src"):
self.src_folder = src_folder
self.png_files = []
def load_png_list(self):
"""加载src文件夹中的所有PNG文件"""
if not os.path.exists(self.src_folder):
os.makedirs(self.src_folder)
print(f"文件夹 {self.src_folder} 不存在,已创建。")
return []
self.png_files = [f for f in os.listdir(self.src_folder) if f.endswith(".png")]
print(f"加载PNG文件列表: {self.png_files}")
return self.png_files
def check_image_on_screen(self, image_path, confidence=0.8):
"""检查当前屏幕是否存在图片"""
location = pyautogui.locateOnScreen(image_path, confidence=confidence)
if location:
print(f'图片: {image_path} 找到,坐标: {location}')
return location
else:
print(f'没有找到图片: {image_path}')
return None
def click_image_center(self, image_path, confidence=0.8):
"""点击图片中心"""
location = self.check_image_on_screen(image_path, confidence)
if location:
button_x, button_y = pyautogui.center(location)
print(f'图片中心坐标: {button_x}, {button_y}')
pyautogui.click(button_x, button_y)
return True
return False
def move_mouse_to(self, x, y):
"""移动鼠标到指定坐标"""
pyautogui.moveTo(x, y)
print(f'鼠标移动到坐标: {x}, {y}')
def mouse_click(self):
"""鼠标点击"""
pyautogui.click()
print('鼠标点击')
def scroll_mouse(self, delta):
"""滚动鼠标滚轮"""
pyautogui.scroll(delta)
print(f'鼠标滚轮滚动: {delta}')
def main():
# 创建自动化点击器实例
auto_clicker = AutoClicker()
# 加载PNG文件
auto_clicker.load_png_list()
# 创建GUI
root = tk.Tk()
root.title("自动化点击器")
root.geometry("480x320")
button_font = ("Arial", 12)
# 修改背景色和文字颜色
button_bg_color = "lightblue"
button_fg_color = "black"
button_border_width = 2
# 测试按钮
def test_check_image():
file = auto_clicker.png_files[0] if auto_clicker.png_files else "1.png"
location = auto_clicker.check_image_on_screen(os.path.join(auto_clicker.src_folder, file))
if location:
messagebox.showinfo("测试结果", f"图片 {file} 找到!")
else:
messagebox.showerror("测试结果", f"图片 {file} 未找到!")
def test_click_image():
file = auto_clicker.png_files[0] if auto_clicker.png_files else "1.png"
if auto_clicker.click_image_center(os.path.join(auto_clicker.src_folder, file)):
messagebox.showinfo("测试结果", f"点击图片 {file} 成功!")
else:
messagebox.showerror("测试结果", f"点击图片 {file} 失败!")
def test_move_mouse():
auto_clicker.move_mouse_to(100, 100)
messagebox.showinfo("测试结果", "鼠标已移动到 (100, 100)")
def test_mouse_click():
auto_clicker.mouse_click()
messagebox.showinfo("测试结果", "鼠标点击完成")
def test_scroll_mouse():
auto_clicker.scroll_mouse(10)
messagebox.showinfo("测试结果", "鼠标滚轮滚动完成")
def start_tasks():
while True:
# 在这里添加你的任务代码
print("开始任务")
time.sleep(1)
print("任务结束")
time.sleep(1)
# 创建按钮
tk.Button(root, text="开始任务", command=start_tasks, font=button_font, bg=button_bg_color,
fg=button_fg_color, bd=button_border_width).pack(pady=5)
tk.Button(root, text="测试检查图片", command=test_check_image, font=button_font, bg=button_bg_color,
fg=button_fg_color, bd=button_border_width).pack(pady=5)
tk.Button(root, text="测试点击图片", command=test_click_image, font=button_font, bg=button_bg_color,
fg=button_fg_color, bd=button_border_width).pack(pady=5)
tk.Button(root, text="测试移动鼠标", command=test_move_mouse, font=button_font, bg=button_bg_color,
fg=button_fg_color, bd=button_border_width).pack(pady=5)
tk.Button(root, text="测试鼠标点击", command=test_mouse_click, font=button_font, bg=button_bg_color,
fg=button_fg_color, bd=button_border_width).pack(pady=5)
tk.Button(root, text="测试滚动鼠标", command=test_scroll_mouse, font=button_font, bg=button_bg_color,
fg=button_fg_color, bd=button_border_width).pack(pady=5)
# 启动GUI
root.mainloop()
if __name__ == "__main__":
main()