質問

I keep finding ways to map the backspace key differently, but that's not what I'm after.

I'm in a program writing a python code, and basically I want to write a line of code that causes the program to think someone just hit the Backspace key in the GUI (as the backspace key deletes something)

How I would code in a backspace key stroke?

役に立ちましたか?

解決

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.

他のヒント

and i got it !

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

it backspace !

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

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

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

Updating since this still pops up in search. In Python3 print() can and does work if you use the \<end\> parameter. Meaning sys.stdout.write() and .flush() aren't needed.

eg. print("\b"*len(time_str),end='')

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top