How to print out 'Live' mouse position coordinates using pyautogui?

Question:

I used lots of different source codes, and even copied and pasted but I keep getting random symbols that shift when i move my mouse over them
here is my code…

import pyautogui, time, sys
print('Press Ctrl-C to quit.')
try:
    while True:
        CurserPos = pyautogui.position()
        print('b' * len(CurserPos), end='r')
        sys.stdout.flush()

I will show the output as an image.
I am rather new to Python and would really appreciate some expert advice.
Thanks

Asked By: SovietStormSam

||

Answers:

Code :

import pyautogui
pyautogui.displayMousePosition()

Here is some output :

Press Ctrl-C to quit.
X:  0 Y: 1143 RGB: ( 38,  38,  38)

Here is the video where this is being demonstrated https://youtu.be/dZLyfbSQPXI?t=809

Answered By: Joji Antony

Use pyautogui.displayMousePosition() instead of pyautogui.position()

Answered By: Ax Vex

This code will print the live position of your mouse after every one second.

import pyautogui as py #Import pyautogui
import time #Import Time

while True: #Start loop
    print (py.position())
    time.sleep(1)

Pyautogui can programmatically control the mouse & keyboard.

More information about it can be found here https://pypi.org/project/PyAutoGUI/

Answered By: Henul

If you want the coordinates of displayMousePosition stored in a variable, try this:

import pyautogui

def getMousePosition():
    pyautogui.displayMousePosition()
    coords = pyautogui.position()
    return coords
Answered By: Zack Plauché
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.