Python get mouse x, y position on click - python

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)?

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.

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

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

I made this the other day.
It a function to get color or pos on right click / left click:
#Add Any helpfull stuff in functions here for later use
def GetMouseInfos(WhatToGet="leaving emety will get you x and y", GetXOnly=False, GetYOnly=False, GetColor=False, Key='Right', OverrideKey=False):#gets color of whats under Key cursor on right click
try:
import win32api
except ModuleNotFoundError:
print("win32api not found, to install do pip install pywin32")
try:
import time
except ModuleNotFoundError:
print("time not found, to install do pip install time?")
try:
import pyautogui
except ModuleNotFoundError:
print("py auto gui not found, to install do pip install pyautogui")
#--------------------------------------------------------------
#above checks if needed modules are installed if not tells user
#code below is to get all varibles needed
#---------------------------------------------------------------
print(WhatToGet)
if OverrideKey:
Key_To_click = Key
if Key == 'Left':
Key_To_click = 0x01
if Key == 'Right':
Key_To_click = 0x02
if Key == 'Wheel':
Key_To_click = 0x04
state_left = win32api.GetKeyState(Key_To_click) # Left button up = 0 or 1. Button down = -127 or -128
IsTrue = True
while IsTrue:
a = win32api.GetKeyState(Key_To_click)
if a != state_left: # Button state changed
state_left = a
if a < 0:
global Xpos, Ypos
Xpos, Ypos = win32api.GetCursorPos()
x, y = pyautogui.position()
pixelColor = pyautogui.screenshot().getpixel((x, y))
else:
posnowX, posnowY = win32api.GetCursorPos()
win32api.SetCursorPos((posnowX, posnowY))
IsTrue = False#remove this for it to keep giving coords on click without it just quitting after 1 click
time.sleep(0.001)
#--------------------------------------------------------------------
#The Code above is the code to get all varibles and code below is for the user to get what he wants
#--------------------------------------------------------------------
if GetXOnly: #Checks if we should get Only X (def options) the command to do this would be GetKeyInfos("Click To get X ONLY", True)
if GetYOnly:
return(Xpos , Ypos)
if GetColor:
return(Xpos, pixelColor)
return(Xpos)
if GetYOnly: #Checks if we should get Only Y (def options) the command to do this would be GetKeyInfos("Click To get X ONLY",False, True)
if GetXOnly:
return(Xpos , Ypos)
if GetColor:
return(Ypos, pixelColor)
return(Ypos)
if GetColor:
return(pixelColor) #Checks
return(Xpos, Ypos)
# getKeyinfos("Anything here without any other guidelines will give u x and y only on right click")

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

Here is an example for canvas with tkinter:
def callback(event):
print("clicked at: ", event.x, event.y)
canvas.bind("<Button-1>", callback)

For turtle :
def get_mouse_click_coor(x, y):
print(x, y)
turtle.onscreenclick(get_mouse_click_coor)

Capture the coordinates (x,y) of the mouse, when clicking with the left button, without using Tkinter?
It's simple:
Install pynput (use pip install pynput (without the 'i').
Copy and paste this code into your editor:
from pynput.mouse import Listener
def on_click(x, y, button, pressed):
x = x
y = y
print('X =', x, '\nY =', y)
with Listener(on_click=on_click) as listener:
listener.join()
I hope this help you =D

You all are making it too hard, its just as easy as:
import pyautogui as pg
pos = pg.position()
# for x pos
print(pos[0])
# for y pos
print(pos[1])

Related

Mouse Coordinates in Graph Window

I'm trying to figure out how to find the mouse coordinates when clicking on the graph window a few times.
So far I've tried
mx ,my = win.mouseX(), win.mouseY() and it tells me that the Nonetype is not callable. I've seen other posts involving tkinter, but I am not using that library even though I see that it's easier. Some more example code is as follows:
from graphics import *
win = GraphWin("test", 300, 300)
for i in range(3):
win.getMouse()
mx, my = win.mouseX(), win.mouseY()
print(mx,my)
I want the above code to have the user click on the window and print the regarding mouse coordinates. Eventually I want to store these coordinates, but I think I can figure that out.
win.getMouse() returns a Point which you can get coordinates from like this:
from graphics import *
win = GraphWin("test", 300, 300)
for i in range(3):
point = win.getMouse()
mx, my = point.getX(), point.getY()
print(mx,my)

Is ipyturtle missing much of the full turtle-graphics module?

I'm trying Turtle for the first time and running into some trouble. I'm using ipyturtle, a widget that lets you use Turtle inline on a Jupyter notebook. It seems to be missing some commands. For example:
from ipyturtle import Turtle
t = Turtle()
t
size = 10
angle = 20
t.reset()
for a in range(10):
for i in range(100):
t.right(1)
t.forward(i/size)
t.home()
t.right(a*angle)
draws the first line, then throws the error:
AttributeError: 'Turtle' object has no attribute 'home'
It also seems to be missing goto(), speed(), among other key commands. Am I doing something wrong? If it is missing commands, how can you tell? I've used Python a fair amount in an engineering context but am unfamiliar with Github. I'd really appreciate an explanation of how someone navigating the page I linked above might sniff out a list of available commands.
I've tried running the following very similar block of code on Repl.it and it works fine:
from turtle import Turtle
t = Turtle()
size = 15
angle = 20
for a in range(1, 19):
for i in range(100):
t.right(1)
t.forward(i/size)
t.home()
t.right(a*angle)
Thanks in advance for your help!
Looking at the ipyturtle code, these are the turle methods supported:
position(self)
forward(self, length)
back(self, length)
heading(self)
goto(self, x, y=None)
setpos(self, x, y=None)
setposition(self, x, y=None)
left(self, degree=None)
right(self, degree=None)
penup(self)
pendown(self)
isdown(self)
hideturtle(self)
showturtle(self)
isvisible(self)
reset(self)
pencolor(self,r=-1,g=-1,b=-1)
So you're right about home() and speed() but goto() does appear to be there. There also appears to be only one name for each command, not the wealth of aliases available in Python turtle (e.g. forward(), fd()).
The t.home() call can be replaced by:
t.goto(0, 0)
t.setheading(0)
But in your example, you immediately do right() afterward so we can combine that into the setheading(). I believe the following should work in ipyturtle, Repl.it and standard Python:
from turtle import Turtle
size = 10
angle = 20
t = Turtle()
for a in range(1, 19):
for i in range(100):
t.right(1)
t.forward(i / size)
t.goto(0, 0)
t.setheading(-a * angle)

Pyautogui scroll fine tuning?

The pyautogui scroll amount value 1 is too small, 2 is to big for a specific task I want to do. Is there a way to scroll inbetween? I tried 1.5, but it didn't work.
I'm on OSX 10.13 and I can certainly scroll with more precision than what pyautogui is doing, when using the trackpad.
This is an issue that has been annoying me, so I took a look at the pyautogui source code and was able to solve the problem. This will probably be quite a long answer; I'll try to explain every step in detail. Note that this only works for Mac. (scroll to the bottom if you want the answer, not the explanation)
First, here is the source code for the scroll function:
def _scroll(clicks, x=None, y=None):
_vscroll(clicks, x, y)
def _vscroll(clicks, x=None, y=None):
_moveTo(x, y)
clicks = int(clicks)
for _ in range(abs(clicks) // 10):
scrollWheelEvent = Quartz.CGEventCreateScrollWheelEvent(
None, # no source
Quartz.kCGScrollEventUnitLine, # units
1, # wheelCount (number of dimensions)
10 if clicks >= 0 else -10) # vertical movement
Quartz.CGEventPost(Quartz.kCGHIDEventTap, scrollWheelEvent)
scrollWheelEvent = Quartz.CGEventCreateScrollWheelEvent(
None, # no source
Quartz.kCGScrollEventUnitLine, # units
1, # wheelCount (number of dimensions)
clicks % 10 if clicks >= 0 else -1 * (-clicks % 10)) # vertical movement
Quartz.CGEventPost(Quartz.kCGHIDEventTap, scrollWheelEvent)
Let's break this down:
1.
def _scroll(clicks, x=None, y=None):
_vscroll(clicks, x, y)
This is just a wrapper for the _vscroll function, simple enough.
2.
The main thing to realise is that pyautogui, for Mac, uses Quartz Core Graphics, all it does is provide a simpler, more readable wrapper for the Quartz code.
With the scroll function, what it is doing is creating a scroll event:
scrollWheelEvent = Quartz.CGEventCreateScrollWheelEvent
And then posting it:
Quartz.CGEventPost(Quartz.kCGHIDEventTap, scrollWheelEvent)
Ignore the details of the posting, we won't be changing any of that.
To me, it seems as if this code repeats itself, and I have no clue why any of the code after the for loop is included. I deleted this from my source code and everything works; If anyone knows why this code is included, please comment below and correct me.
3.
So we are left with the following code (ignoring the mouse moveTo, which has nothing to do with the scrolling itself):
clicks = int(clicks)
for _ in range(abs(clicks) // 10):
scrollWheelEvent = Quartz.CGEventCreateScrollWheelEvent(
None, # no source
Quartz.kCGScrollEventUnitLine, # units
1, # wheelCount (number of dimensions)
10 if clicks >= 0 else -10) # vertical movement
Quartz.CGEventPost(Quartz.kCGHIDEventTap, scrollWheelEvent)
The format of a CGEventCreateScrollWheelEvent is the following:
Quartz.CGEventCreateScrollWheelEvent(source, units, wheelCount, scroll distance)
The source in this case is None, don't worry about that, and we are only dealing with 1 wheel, so wheelCount is 1.
What the source code is doing, therefore, is scrolling a distance of ±10 Quartz.kCGScrollEventUnitLine, which are your computers units for one 'scroll'. It repeats this in a for loop for however many times you specify because the system can bug if too many scroll units are sent at once.
Therefore, the minimum one can scroll on pyautogui is one iteration of this loop, which sends one computer unit. The problem is that these units are too big for fine scrolling.
SOLUTION
We need to change the minimum value we can send. Currently it is 1 Quartz.kCGScrollEventUnitLine, but we can change these to base units by replacing them with a zero. I also see no need to floor divide clicks (in range(abs(clicks) // 10)) and then send 10 scroll units.
We can change these two parts, and remove the unnecessary repetition:
def _scroll(clicks, x=None, y=None):
_vscroll(clicks, x, y)
def _vscroll(clicks, x=None, y=None):
_moveTo(x, y)
clicks = int(clicks)
for _ in range(abs(clicks)): # <------------------------------------
scrollWheelEvent = Quartz.CGEventCreateScrollWheelEvent(
None, # no source
0, # units <------------------------------------------------
1, # wheelCount (number of dimensions)
1 if clicks >= 0 else -1) # vertical movement <--------------
Quartz.CGEventPost(Quartz.kCGHIDEventTap, scrollWheelEvent)
If you don't feel comfortable editing the source code itself, you can use these functions in your code directly, skipping out the need for pyautogui. Just have pyobjc installed (which you'll have anyway if you use pyautogui), remove _moveTo(x, y) and the keyword arguments, and use the following imports:
from Quartz.CoreGraphics import CGEventCreateScrollWheelEvent, CGEventPost, kCGHIDEventTap
I realise this answer is a bit late, but I came looking for answers to this problem and saw your question; When I solved the problem I thought I would share the knowledge.
I really struggled with this one, so I thought I'd post my solution for Windows.
After a quick pip install pywin32, I got access to the necessary win32api & win32con, among others.
NOTE: The last time I checked, pywin32 was only supported for:
Python :: 2.7
Python :: 3.5
Python :: 3.6
Python :: 3.7
import time
import win32api
import win32con
def scroll(clicks=0, delta_x=0, delta_y=0, delay_between_ticks=0):
"""
Source: https://learn.microsoft.com/en-gb/windows/win32/api/winuser/nf-winuser-mouse_event?redirectedfrom=MSDN
void mouse_event(
DWORD dwFlags,
DWORD dx,
DWORD dy,
DWORD dwData,
ULONG_PTR dwExtraInfo
);
If dwFlags contains MOUSEEVENTF_WHEEL,
then dwData specifies the amount of wheel movement.
A positive value indicates that the wheel was rotated forward, away from the user;
A negative value indicates that the wheel was rotated backward, toward the user.
One wheel click is defined as WHEEL_DELTA, which is 120.
:param delay_between_ticks:
:param delta_y:
:param delta_x:
:param clicks:
:return:
"""
if clicks > 0:
increment = win32con.WHEEL_DELTA
else:
increment = win32con.WHEEL_DELTA * -1
for _ in range(abs(clicks)):
win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, delta_x, delta_y, increment, 0)
time.sleep(delay_between_ticks)
Then, after defining
click_point = x_position, y_position
and then using
pyautogui.moveTo(x=click_point[0], y=click_point[1], duration=0.25)
to make sure that my mouse is in the correct location. I just call the above scroll function:
scroll(-4, 0.1)
to scroll down 4 ticks with a 100ms delay between ticks.

Mouse Position Python Tkinter

Is there a way to get the position of the mouse and set it as a var?
You could set up a callback to react to <Motion> events:
import Tkinter as tk
root = tk.Tk()
def motion(event):
x, y = event.x, event.y
print('{}, {}'.format(x, y))
root.bind('<Motion>', motion)
root.mainloop()
I'm not sure what kind of variable you want. Above, I set local variables x and y to the mouse coordinates.
If you make motion a class method, then you could set instance attributes self.x and self.y to the mouse coordinates, which could then be accessible from other class methods.
At any point in time you can use the method winfo_pointerx and winfo_pointery to get the x,y coordinates relative to the root window. To convert that to absolute screen coordinates you can get the winfo_pointerx or winfo_pointery, and from that subtract the respective winfo_rootx or winfo_rooty
For example:
root = tk.Tk()
...
x = root.winfo_pointerx()
y = root.winfo_pointery()
abs_coord_x = root.winfo_pointerx() - root.winfo_rootx()
abs_coord_y = root.winfo_pointery() - root.winfo_rooty()
Personally, I prefer to use pyautogui, even in combination with Tkinter. It is not limited to Tkinter app, but works on the whole screen, even on dual screen configuration.
import pyautogui
x, y = pyautogui.position()
In case you want to save various positions, add an on-click event.
I know original question is about Tkinter.
I would like to improve Bryan's answer, as that only works if you have 1 monitor, but if you have multiple monitors, it will always use your coordinates relative to your main monitor. in order to find it relative to both monitors, and get the accurate position, then use vroot, instead of root, like this
root = tk.Tk()
...
x = root.winfo_pointerx()
y = root.winfo_pointery()
abs_coord_x = root.winfo_pointerx() - root.winfo_vrootx()
abs_coord_y = root.winfo_pointery() - root.winfo_vrooty()

Get window position and size in python with Xlib

I need to find window position and size, but I cannot figure out how. For example if I try:
id.get_geometry() # "id" is Xlib.display.Window
I get something like this:
data = {'height': 2540,
'width': 1440,
'depth': 24,
'y': 0, 'x': 0,
'border_width': 0
'root': <Xlib.display.Window 0x0000026a>
'sequence_number': 63}
I need to find window position and size, so my problem is: "y", "x" and "border_width" are always 0; even worse, "height" and "width" are returned without window frame.
In this case on my X screen (its dimensions are 4400x2560) I expected x=1280, y=0, width=1440, height=2560.
In other words I'm looking for python equivalent for:
#!/bin/bash
id=$1
wmiface framePosition $id
wmiface frameSize $id
If you think Xlib is not what I want, feel free to offer non-Xlib solution in python if it can take window id as argument (like the bash script above). Obvious workaround to use output of the bash script in python code does not feel right.
You are probably using reparenting window manager, and because of this id window has zero x and y. Check coordinates of parent window (which is window manager frame)
Liss posted the following solution as a comment:
from ewmh import EWMH
ewmh = EWMH()
def frame(client):
frame = client
while frame.query_tree().parent != ewmh.root:
frame = frame.query_tree().parent
return frame
for client in ewmh.getClientList():
print frame(client).get_geometry()
I'm copying it here because answers should contain the actual answer, and to prevent link rot.
Here's what I came up with that seems to work well:
from collections import namedtuple
import Xlib.display
disp = Xlib.display.Display()
root = disp.screen().root
MyGeom = namedtuple('MyGeom', 'x y height width')
def get_absolute_geometry(win):
"""
Returns the (x, y, height, width) of a window relative to the top-left
of the screen.
"""
geom = win.get_geometry()
(x, y) = (geom.x, geom.y)
while True:
parent = win.query_tree().parent
pgeom = parent.get_geometry()
x += pgeom.x
y += pgeom.y
if parent.id == root.id:
break
win = parent
return MyGeom(x, y, geom.height, geom.width)
Full example here.
In the same idea as #mgalgs, but more direct, I ask the root window to translate the (0,0) coordinate of the target window :
# assuming targetWindow is the window you want to know the position of
geometry = targetWindow.get_geometry()
position = geometry.root.translate_coords(targetWindow.id, 0, 0)
# coordinates are in position.x and position.y
# if you are not interested in the geometry, you can do directly
import Xlib.display
position = Xlib.display.Display().screen().root.translate_coords(targetWindow.id, 0, 0)
This gives the position of the client region of the targeted window (ie. without borders, title bar and shadow decoration created by the window manage). If you want to include them, replace targetWindow with targetWindow.query_tree().parent (or second parent).
Tested with KUbuntu 20.04 (ie KDE, Plasma and KWin decoration).

Categories

Resources