Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
每次读取和打印一个字符-getche()和Python中的退格_Python_Python 3.x_Character_Backspace_Getch - Fatal编程技术网

每次读取和打印一个字符-getche()和Python中的退格

每次读取和打印一个字符-getche()和Python中的退格,python,python-3.x,character,backspace,getch,Python,Python 3.x,Character,Backspace,Getch,我想创建打字培训计划。我需要一个函数,它可以立即读取和打印用户点击的每个字符,比如getche() 我试过使用getchefrom,但它不能很好地处理退格。当我点击backspace时,它会打印^?到控制台,我希望它删除字符。操作非常清楚 尝试使用getche 不要这样做,因为getche()被记录为具有您说不想要的行为 调用getch(),并根据您的要求负责“回显”或以其他方式维护显示器 例如,此代码实现了您想要的功能: from getch import getch def pr(s):

我想创建打字培训计划。我需要一个函数,它可以立即读取和打印用户点击的每个字符,比如getche()

我试过使用getchefrom,但它不能很好地处理退格。当我点击backspace时,它会打印^?到控制台,我希望它删除字符。

操作非常清楚

尝试使用getche

不要这样做,因为
getche()
被记录为具有您说不想要的行为

调用
getch()
,并根据您的要求负责“回显”或以其他方式维护显示器

例如,此代码实现了您想要的功能:

from getch import getch


def pr(s):
    print(s, end='', flush=True)


def get_word():
    DELETE = 127  # ASCII code
    word = ''
    c = ''
    while not c.isspace():
        c = getch()
        if ord(c) == DELETE:
            pr('\r' + ' ' * len(word) + '\r')
            word = word[:-1]
            pr(word)
        if c.isprintable():
            word += c
            pr(c)
    print('\n')
    return word
从官方的角度来看,它的定义是:

curses模块提供到curses库的接口,curses库是便携式高级终端处理的事实标准

你说你想写一个打字训练程序,我认为最好的解决办法是使用
诅咒库来完成这样的任务

在UNIX系统上,它附带python的默认安装,如果您的目标是windows系统,我发现可以大大增加支持

基本上,您可以在官方文档的页面中找到如何操作指南

下面是创建文本框小部件的示例用法 该模块应该对您非常有用

导入诅咒
从诅咒导入包装器
从curses.textpad导入文本框,矩形
def干管(stdscr):
addstr(0,0,“输入IM消息:(点击Ctrl-G发送)”)
editwin=诅咒。newwin(5,30,2,1)
矩形(stdscr,1,0,1+5+1,1+30+1)
stdscr.refresh()
box=Textbox(editwin)
#让用户编辑,直到按下Ctrl-G键。
box.edit()
#获取结果内容
message=box.gather()
打印(信息)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
包装器(主)
下面是使用
windows诅咒
模块时的情况

你可以使用这个库做很多事情,我建议你继续阅读我提供的链接上的文档