1

The code below should continuously display the coordinates of the mouse. I am using Pycharm. When run in the terminal it works as intended, but when run using the "Run" command it does not display any output.

#! python3
# mouseNow.py - Displays the mouse cursor's current position.

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:
    print('\nDone.')

2 Answers 2

2

I got it to work by modifying the Run/Debug Configurations. Under execution there is an option to "Emulate terminal in output console". When this is selected the output displays as expected.

enter image description here

I still don't know why PyCharm ran the code differently to start with.

Sign up to request clarification or add additional context in comments.

1 Comment

pyautogui has a built-in tool for exactly this problem: import pyautogui; pyautogui.displayMousePosition()
0

you should not run your code in RUN section, rather, if you want to see the result of your code (where the cursor position is) you either have to run it in terminal by typing python path/to/the/python/file or the console.

and you'r answer: The reason why it does not show you the result consecutively , is that, till the time no new action is taken by you(to move the cursor), it does not have any new data(value) to show you.(it's just as similar as writing and running your code in IDE which gives you immediate response for example if you type 2 * 8 it will not need to use print() function ), but if you use IDLE(pycharm) and it's RUN section to run you'r code , it must not have the feature to give your immediate response because IDE, shell, console and Terminal already are available to you.

2 Comments

Thank you for the reply but I'm still confused about it. The script is running an infinite loop so it will always have data to show. It's displaying and deleting the coordinates continuously. If the RUN section will not show the result or give and immediate response then what is it used for?
@K753, think of it in this way, terminal(shell, anything else that gives immediate result) has a hidden print() function that when you type anything it will immediately throws out some results. but because just like the editor section which you need to print something, Run section is just made for that, and is not like , you know... the shell or terminal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.