11

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

Community
  • 1
  • 1
SovietStormSam
  • 123
  • 1
  • 1
  • 7
  • I'm not sure exactly what you want printed out, but I don't think the first argument of the print function is what you want. `'\b' * len(CurserPos)` will "print" the backspace character ('\b') two times (note that CurserPos will always be 2 since `pyautogui.position()` always returns a tuple of two items). – D. Gillis Jun 14 '17 at 10:40

5 Answers5

22

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

Joji Antony
  • 378
  • 2
  • 10
6

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/

Henul
  • 192
  • 1
  • 10
  • 5
    Welcome to StackOverflow. While this code may answer the question, providing additional context regarding *how* and/or *why* it solves the problem would improve the answer's long-term value. – Sven Eberth Jul 12 '21 at 22:19
  • I use this while loop often but how can I only get a print if there is a click? I want to store the position of four click points as tuple. – Richard Modad May 14 '22 at 22:14
0

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

Ax Vex
  • 1
  • 3
0

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

import pyautogui

def getMousePosition():
    pyautogui.displayMousePosition()
    coords = pyautogui.position()
    return coords
Zack Plauché
  • 3,307
  • 4
  • 18
  • 34
0

I didn´t like to be printing in the terminal the position of the mouse so I made a simple draggable GUI to display mouse location. Hope it helps: https://github.com/Setibs/Mouse-position-GUI

setibs
  • 24
  • 3
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/34821343) – doneforaiur Aug 16 '23 at 06:36