Jan-10-2023, 10:45 PM
Care to enlighten about the cause of the problem? I have no lag in the mouse curser when the listener is running, but I am not calling the pyautogui function. Is locateCenterOnScreen() really slow? You could run your click code in a separate thread to keep the mouse responsive.
This code runs the click code in its own thread, so it doesn't bog down the mouse. I use sleep(1) to simulate some slow code that would block the mouse from moving,
This code runs the click code in its own thread, so it doesn't bog down the mouse. I use sleep(1) to simulate some slow code that would block the mouse from moving,
from pynput.mouse import Listener import tkinter as tk import threading from time import sleep def on_click_worker(x1, y1): """The slow code you want to run each time you click the mouse.""" sleep(1) # Simulates slow code print(x1, y1) # replace with your image finding/process killing code def on_click(x1, y1, button, pressed): """Mouse listener event handler. Return False to stop listener.""" if window.winfo_containing(x1, y1) in (start_btn, exit_btn): # Stop listener if user clicks start or exit buttons return False if pressed: threading.Thread(target=on_click_worker, args=(x1, y1)).start() return True def mouse_listener(): """Worker function for mouse listener thread""" with Listener(on_click=on_click) as listener: listener.join() def start_stop(): """Start/Stop button callback""" if start_btn["text"] == "Start": start_btn["text"] = "Stop" threading.Thread(target=mouse_listener).start() else: start_btn["text"] = "Start" def exit_program(): """Exit button callback""" window.quit() font = ("Helvetica", 24) window = tk.Tk() start_btn = tk.Button(window, text="Start", font=font, width=5, command=start_stop) start_btn.pack(side=tk.LEFT, ipadx=20, ipady=20) exit_btn = tk.Button(window, text="Exit", font=font, command=exit_program) exit_btn.pack(side=tk.LEFT, ipadx=20, ipady=20) window.mainloop()