Wrong output when trying to print out mouse position
<p>I'm trying to make a function which continually prints out the mouse position constantly until stopped.
import pyautogui</p>

<pre><code>import pyautogui

print('Press CTRL + "c" to stop')

while True:
    try:
        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')
        break
</code></pre>

<p>The expected output should look something like this:</p>

<p><em>X: 265 Y:634</em>
<em>only one line continually refreshed</em></p>

<p>But this is what I'm getting instead:</p>

<p>XXXXXXXXXXXXXXXXXXX:  665 Y:  587</p>

<p>XXXXXXXXXXXXXXXXX:  665 Y:  587 </p>

<p>XXXXXXXXXXXXXXXXXXXX:  665 Y:  587 </p>

<p>XXXXXXXXXX:  718 Y:  598 </p>

<p>XXXXXXXXXXXX: 1268 Y:  766 </p>

<p><em>remove the \b characters</em>
    import pyautogui</p>

<pre><code>print('Press CTRL + "c" to stop')

while True:
try:
    x, y = pyautogui.position()
    positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
    print(positionStr)
    print('\b' * len(positionStr), end = '', flush = True)
except KeyboardInterrupt:
    print('\nDone')
    break
</code></pre>

<p><em>X:  830 Y:  543</em></p>

<p><em>X:  830 Y:  543</em></p>

<p><em>X:  830 Y:  543</em></p>

<p><em>X:  830 Y:  543</em></p>

<p>Done</p>

<p>You are not backspacing enough characters. You forgot to account for the extra space "end" character.  Of course you should be able to leave out the <code>end</code> parameter entirely.</p>

python debugging pyautogui