K753
K753

Reputation: 388

Python script doesn't run correctly in PyCharm but runs in terminal

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.')

Upvotes: 1

Views: 979

Answers (2)

moh80s
moh80s

Reputation: 761

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.

Upvotes: 0

K753
K753

Reputation: 388

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.

Upvotes: 2

Related Questions