我正在尝试制作一个不断打印鼠标位置直到停止的功能。导入pyautogui
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
预期的输出应如下所示:
X: 265 Y:634 只有一行不断刷新
但这是我得到的:
XXXXXXXXXXXXXXXXXXX:665 Y:587
XXXXXXXXXXXXXXXXXX:665 Y:587
XXXXXXXXXXXXXXXXXXXXX:665 Y:587
XXXXXXXXXX:718 Y:598
XXXXXXXXXXXX:1268 Y:766
删除 \b 字符 导入 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)
print('\b' * len(positionStr), end = '', flush = True)
except KeyboardInterrupt:
print('\nDone')
break
X:830 Y:543
X:830 Y:543
X:830 Y:543
X:830 Y:543
完毕