lys*_*igk 6 python curses encoding ncurses python-curses
我正在尝试使用cursestextpad.Textbox()函数进行文本输入。到目前为止,一切工作正常,但是,某些键无法识别,包括部分符号(\xc2\xa7)和所有德语变音符号(\xc3\xa4/\xc3\xb6/\xc3\xbc)。我想这与文本编码有某种关系,但我不知道如何解决这个问题。我的德语键盘布局与input().
这是一些最小的例子:
\n\n    import curses\n    import curses.textpad as textpad\n\n    try:\n        stdtscr = curses.initscr()\n        curses.cbreak()\n        stdtscr.keypad(1)\n        curses.noecho()\n\n        textpad.Textbox(stdtscr).edit()\n\n    finally:\n        curses.nocbreak()\n        stdtscr.keypad(0)\n        curses.echo()\n        curses.endwin()\n就像在 C 中一样,您应该初始化语言环境。Python 文档中对此进行了详细说明:
\n\n\n\n\n\n\n从版本 5.4开始,ncurses 库决定如何使用 nl_langinfo 函数解释非 ASCII 数据。这意味着您必须在应用程序中调用 locale.setlocale() 并使用 system\xe2\x80\x99s 可用编码之一对 Unicode 字符串进行编码。
\n
   The  library uses the locale which the calling program has\n   initialized.  That is normally done with setlocale:\n\n           setlocale(LC_ALL, "");\n\n If the locale is not initialized, the library  assumes  that\n characters are printable as in ISO-8859-1, to work with cer-\n tain legacy programs.  You should initialize the locale  and\n not  rely on specific details of the library when the locale\n has not been setup.\n针对后续评论,textpad.py在任何情况下都不需要 UTF-8 输入。本质上,它“验证”其输入,确定它不是 ASCII,并在不是时忽略它。
\n\nPython 的curses 绑定提供了一个接口wgetch,它(通过ncurses)给出了UTF-8 的各个字节。(X/Open Curses 指定了一个不同的函数wget_wch,Python 没有绑定该函数)。
textpad.py可以修改为通过将字节组装成 Unicode 值来解决诅咒绑定,但您需要setlocale作为第一步。