使用 Python,我正在尝试编写一个脚本,该脚本将在您按下空格键时将所有键入的字符转换为“a”。例如,我输入“python”,然后输入空格,然后“python”将转换为“aaaaaa”。
import argparse
import curses
import time
# Main Function
def main():
screen=curses.initscr()
curses.cbreak()
screen.keypad(1)
curses.echo()
str_txt=''
count = 0
while True:
s=screen.getch()
if s != ord(' ') and s != ord('\x1b') and s != curses.KEY_BACKSPACE and s != curses.KEY_ENTER:
str_txt += chr(int(s))
count+=1
if s == ord(' '):
dim = screen.getyx()
h = 'a'*len(str_txt)+' '
screen.addstr(dim[0],dim[1]-count-1, h)
count=0
str_txt=''
screen.refresh()
if s == curses.KEY_ENTER or s==10 or s==13:
dim = screen.getyx()
screen.move(dim[0]+1,0)
screen.refresh()
#if s == curses.KEY_BACKSPACE:
# dim = screen.getyx()
# screen.move(dim[0],dim[1])
# screen.refresh()
if s == ord('\x1b'):
curses.endwin()
break
if __name__ == "__main__":
main()
上面的代码适用于第一行,但是,在第二行中,每当我按空格键时,我都会在第 22 行收到一个错误,提示“_curses.error: addstr() returned ERR”
编辑:当我将 screen.addstr(dim[0],dim 1 -count-1, h) 更改为 screen.addstr(dim[0],dim 1 -count, h) 时,错误被消除但输出不是我想要的是。我已附上输出供您参考。