Python get mouse x, y position on click

99,319

Solution 1

There are a number of libraries you could use. Here are two third party ones:

Using PyAutoGui

A powerful GUI automation library allows you to get screen size, control the mouse, keyboard and more.

To get the position you just need to use the position() function. Here is an example:

>>>import pyautogui
>>>pyautogui.position()
(1358, 146)
>>>

Where 1358 is the X position and 146 is the Y position.

Relavent link to the documentation

Using Pynput

Another (more minimalistic) library is Pynput:

>>> from pynput.mouse import Controller
>>> mouse = Controller()
>>> mouse.position
(1182, 153)
>>>

Where 1182 is the X position and 153 is the second.

Documentation

This library is quite easy to learn, does not require dependencies, making this library ideal for small tasks like this (where PyAutoGui would be an overkill). Once again though, it does not provide so many features though.

Windows Specific:

For platform dependant, but default library options (though you may still consider them overkills) can be found here: Getting cursor position in Python.

Solution 2

Using PyMouse:

>>> import pymouse
>>> mouse = pymouse.PyMouse()
>>> mouse.position()
(231L, 479L)

Solution 3

As an example, for plot or images, it is possible to use the matplotlib tool called ginput. At every click of the mouse the [x,y] coordinates of the selected point are stored in a variable.

# show image
fig, ax=plt.subplots()
ax.imshow(img)
# select point
yroi = plt.ginput(0,0)

using ginput(0,0) you can select any points on the plot or image.

here the link for the ginput documentation

https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.ginput.html

Solution 4

Use pygame

import pygame
mouse_pos = pygame.mouse.get_pos()

This returns the x and y position of the mouse.

See this website: https://www.pygame.org/docs/ref/mouse.html#pygame.mouse.set_pos

Solution 5

For turtle :

def get_mouse_click_coor(x, y):
    print(x, y)
turtle.onscreenclick(get_mouse_click_coor)
Share:
99,319

Related videos on Youtube

Author by

ben_bo

Updated on December 15, 2021

Comments

  • ben_bo over 1 year

    Coming from IDL, I find it quite hard in python to get the x-y position of the mouse on a single left click using a method that is not an overkill as in tkinter. Does anyone know about a python package that contains a method simply returning x-y when the mouse is clicked (similar to the cursor method in IDL)?

    • Gabriel
      Gabriel about 6 years
      I've voted to re-open this question. It was closed as "too broad" which I disagree with. The OP is asking for a method to obtain coordinates on mouse click without having to resort to tkinter overkill (e.g.: stackoverflow.com/questions/5501192/…) What's too broad about this?
  • Kevin
    Kevin over 8 years
    See also the library PyUserInput, which integrates PyMouse's code and appears to be more current. One of its dependencies, PyHook, is officially 32 bit only, but a third-party 64 bit installer can be found here.
  • Kevin
    Kevin over 8 years
    You can listen for mouse click events by subclassing the PyMouseEvent class. See the "Clickonacci" example on the PyUserInput page.
  • Hadi Farah
    Hadi Farah over 2 years
    if I use this I get pygame.error: video system not initialized, and if I use pygame.init() then I get a constant (0,0) position wherever my cursor is.
  • quicksilver
    quicksilver over 1 year
    add turtle.mainloop() in the end so the screen remains open when you click.
  • mkrieger1
    mkrieger1 about 1 year
    But how to execute this when the mouse is clicked?
  • mkrieger1
    mkrieger1 about 1 year
    This doesn't answer how to get the position when the mouse is clicked.
  • mkrieger1
    mkrieger1 about 1 year
    This doesn't answer how to get the position when the mouse is clicked.
  • hotwheel2007
    hotwheel2007 12 months
    That returns the x & y position of the mouse in a pygame window, not in the entire screen.