click_test.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import os
  2. import pyautogui
  3. import tkinter as tk
  4. from tkinter import messagebox
  5. import time
  6. class AutoClicker:
  7. def __init__(self, src_folder="src"):
  8. self.src_folder = src_folder
  9. self.png_files = []
  10. def load_png_list(self):
  11. """加载src文件夹中的所有PNG文件"""
  12. if not os.path.exists(self.src_folder):
  13. os.makedirs(self.src_folder)
  14. print(f"文件夹 {self.src_folder} 不存在,已创建。")
  15. return []
  16. self.png_files = [f for f in os.listdir(self.src_folder) if f.endswith(".png")]
  17. print(f"加载PNG文件列表: {self.png_files}")
  18. return self.png_files
  19. def check_image_on_screen(self, image_path, confidence=0.8):
  20. """检查当前屏幕是否存在图片"""
  21. location = pyautogui.locateOnScreen(image_path, confidence=confidence)
  22. if location:
  23. print(f'图片: {image_path} 找到,坐标: {location}')
  24. return location
  25. else:
  26. print(f'没有找到图片: {image_path}')
  27. return None
  28. def click_image_center(self, image_path, confidence=0.8):
  29. """点击图片中心"""
  30. location = self.check_image_on_screen(image_path, confidence)
  31. if location:
  32. button_x, button_y = pyautogui.center(location)
  33. print(f'图片中心坐标: {button_x}, {button_y}')
  34. pyautogui.click(button_x, button_y)
  35. return True
  36. return False
  37. def move_mouse_to(self, x, y):
  38. """移动鼠标到指定坐标"""
  39. pyautogui.moveTo(x, y)
  40. print(f'鼠标移动到坐标: {x}, {y}')
  41. def mouse_click(self):
  42. """鼠标点击"""
  43. pyautogui.click()
  44. print('鼠标点击')
  45. def scroll_mouse(self, delta):
  46. """滚动鼠标滚轮"""
  47. pyautogui.scroll(delta)
  48. print(f'鼠标滚轮滚动: {delta}')
  49. def main():
  50. # 创建自动化点击器实例
  51. auto_clicker = AutoClicker()
  52. # 加载PNG文件
  53. auto_clicker.load_png_list()
  54. # 创建GUI
  55. root = tk.Tk()
  56. root.title("自动化点击器")
  57. root.geometry("480x320")
  58. button_font = ("Arial", 12)
  59. # 修改背景色和文字颜色
  60. button_bg_color = "lightblue"
  61. button_fg_color = "black"
  62. button_border_width = 2
  63. # 测试按钮
  64. def test_check_image():
  65. file = auto_clicker.png_files[0] if auto_clicker.png_files else "1.png"
  66. location = auto_clicker.check_image_on_screen(os.path.join(auto_clicker.src_folder, file))
  67. if location:
  68. messagebox.showinfo("测试结果", f"图片 {file} 找到!")
  69. else:
  70. messagebox.showerror("测试结果", f"图片 {file} 未找到!")
  71. def test_click_image():
  72. file = auto_clicker.png_files[0] if auto_clicker.png_files else "1.png"
  73. if auto_clicker.click_image_center(os.path.join(auto_clicker.src_folder, file)):
  74. messagebox.showinfo("测试结果", f"点击图片 {file} 成功!")
  75. else:
  76. messagebox.showerror("测试结果", f"点击图片 {file} 失败!")
  77. def test_move_mouse():
  78. auto_clicker.move_mouse_to(100, 100)
  79. messagebox.showinfo("测试结果", "鼠标已移动到 (100, 100)")
  80. def test_mouse_click():
  81. auto_clicker.mouse_click()
  82. messagebox.showinfo("测试结果", "鼠标点击完成")
  83. def test_scroll_mouse():
  84. auto_clicker.scroll_mouse(10)
  85. messagebox.showinfo("测试结果", "鼠标滚轮滚动完成")
  86. def start_tasks():
  87. while True:
  88. # 在这里添加你的任务代码
  89. print("开始任务")
  90. time.sleep(1)
  91. print("任务结束")
  92. time.sleep(1)
  93. # 创建按钮
  94. tk.Button(root, text="开始任务", command=start_tasks, font=button_font, bg=button_bg_color,
  95. fg=button_fg_color, bd=button_border_width).pack(pady=5)
  96. tk.Button(root, text="测试检查图片", command=test_check_image, font=button_font, bg=button_bg_color,
  97. fg=button_fg_color, bd=button_border_width).pack(pady=5)
  98. tk.Button(root, text="测试点击图片", command=test_click_image, font=button_font, bg=button_bg_color,
  99. fg=button_fg_color, bd=button_border_width).pack(pady=5)
  100. tk.Button(root, text="测试移动鼠标", command=test_move_mouse, font=button_font, bg=button_bg_color,
  101. fg=button_fg_color, bd=button_border_width).pack(pady=5)
  102. tk.Button(root, text="测试鼠标点击", command=test_mouse_click, font=button_font, bg=button_bg_color,
  103. fg=button_fg_color, bd=button_border_width).pack(pady=5)
  104. tk.Button(root, text="测试滚动鼠标", command=test_scroll_mouse, font=button_font, bg=button_bg_color,
  105. fg=button_fg_color, bd=button_border_width).pack(pady=5)
  106. # 启动GUI
  107. root.mainloop()
  108. if __name__ == "__main__":
  109. main()