Python code to cause a backspace keystroke?

£可爱£侵袭症+ 提交于 2019-12-01 16:32:43

The character for backspace is '\b' but it sounds like you want to affect the GUI.

if your program changes the GUI, then simply delete the last character from the active input field.

foo = "abc"
foo = foo + "\b" + "xyz"
print foo
>> abxyz
print len(foo)
>> 7

if key == '\b': delete_selected_points()

and i got it !

print('\b ', end="", flush=True) 
sys.stdout.write('\010')

it backspace !

As other answers have said, use '\b' to backspace. The trick in your case is to use sys.stdout.write instead of print to not get a newline appended. Then wait and print the appropriate number of backspace characters.

import time
import sys

print("Good morning!")
while True:
    time_fmt = "It's %I:%M:%S %p on %A, %b %d, %Y"
    time_str = time.strftime(time_fmt)
    sys.stdout.write(time_str)
    sys.stdout.flush()
    time.sleep(1)
    sys.stdout.write("\b"*len(time_str))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!