Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

In python, write a function to print coordinates on the screen where mouse clicks.

  1. User

    In python, write a function to print coordinates on the screen where mouse clicks.

  2. Assistant

    You can use the pyautogui library in Python to achieve this. The pyautogui library provides functions to control the mouse and keyboard, including getting the current position of the mouse pointer. Here's an example function that will print the coordinates on the screen when the user clicks the mouse:

    import pyautogui
    
    def print_mouse_coordinates():
        while True:
            try:
                x, y = pyautogui.position()
                print(f"Mouse clicked at coordinates: ({x}, {y})")
                # To stop the program, you can use Ctrl + C or close the terminal
            except KeyboardInterrupt:
                break
    
    print_mouse_coordinates()
    

    When you run this function, it will continuously print the coordinates of the mouse pointer whenever a click occurs. To stop the program, you can press Ctrl + C in the terminal or close the terminal window.