1

I'm trying to write a program that will constantly display x- and y- coordinates of the mouse cursor as I move it around(Automate the boring stuff in python page 417).

I have tried adjusting indentation of the Try statements,but I was still receiving indentation error message.

import pyautogui

print('press Ctrl-C to quit.')

try:

    while True:

except KeyboardInterrupt:

     print('\nDone.')

        x,y=pyautogui.position()

        positionStr='X: ' + str(x).rjust(4) + 'Y: ' + str(y).rjust(4)

        print(positionStr,end='')

        print('\b'*len(positionStr),end='',flush=True)

I expected an output of two lines like below:

press Ctrl-C to quit. X:290 Y:424

But the output I got was :

 File "<ipython-input-2-b3f3ee266ed5>", line 6

    except KeyboardInterrupt:
                             ^
IndentationError: expected an indented block
Keatinge
  • 4,330
  • 6
  • 25
  • 44
Loui
  • 97
  • 2
  • 11
  • Possible duplicate of [Python: Expected an indented block](https://stackoverflow.com/questions/21764912/python-expected-an-indented-block) – T Tse May 24 '19 at 03:33
  • See above. Your line of `while True:` must be followed by the body of the loop. – T Tse May 24 '19 at 03:33

1 Answers1

2

The code should be:

import pyautogui

print('press Ctrl-C to quit.')

try:
    while True:
        x,y=pyautogui.position()

        positionStr='X: ' + str(x).rjust(4) + 'Y: ' + str(y).rjust(4)

        print(positionStr,end='')

        print('\b'*len(positionStr),end='',flush=True)

except KeyboardInterrupt:
    pass

You had the loop body in the exception handling block.

Well at least it works now to make the output more useful. What is needed is to only print if the position changed from the last time it was polled and to put in a time.sleep(0.1) in the loop.

import pyautogui
import time

print('press Ctrl-C to quit.')

last_position = None

try:
   while True:
       x,y=position=pyautogui.position()

       if position != last_position:

           positionStr='X: ' + str(x).rjust(4) + 'Y: ' + str(y).rjust(4)
           print(positionStr,end='')
           print('\b'*len(positionStr),end='',flush=True)

       last_position = position
       time.sleep(0.1)

except KeyboardInterrupt:
   pass

This reduces the speed of the loop and it will only print if the position has changed.

Dan D.
  • 73,243
  • 15
  • 104
  • 123
  • Hi Dan D. I already applied your strategy before,but like I said I expect the program to output only two lines,so that as you move the cursor around the coordinates will be changing.But with the technique of placing except Keyboard at the end of the program,the output is unending x- and y- coordinates. – Loui May 24 '19 at 06:54
  • Do you mean that you also want the new output from the program to overwrite the prior output? That can be done but perhaps the additions I propose will reduce the output enough. – Dan D. May 24 '19 at 18:23
  • D,Wao this is better.Thanks – Loui May 25 '19 at 02:16
  • @ Dan D,wao this is better. – Loui May 25 '19 at 02:17