Python curses - textpad.Textbox()无法使用德语umlauts进行键盘输入

4
我正在尝试使用curses的textpad.Textbox()函数进行文本输入。目前一切正常,但有些键无法识别,包括小节符号(§)和所有德语umlauts(ä/ö/ü)。我猜这与文本编码有关,但我不知道如何解决。我的德语键盘布局在input()中可以完美地工作。

这是一个最简示例:

    import curses
    import curses.textpad as textpad

    try:
        stdtscr = curses.initscr()
        curses.cbreak()
        stdtscr.keypad(1)
        curses.noecho()

        textpad.Textbox(stdtscr).edit()

    finally:
        curses.nocbreak()
        stdtscr.keypad(0)
        curses.echo()
        curses.endwin()
1个回答

2
与C语言类似,您应该初始化区域设置。这在Python文档ncurses手册页面中都有详细说明:

5.4版本以来,ncurses库使用nl_langinfo函数来解释非ASCII数据。这意味着您必须在应用程序中调用locale.setlocale()并使用系统可用的编码之一对Unicode字符串进行编码。

   The  library uses the locale which the calling program has
   initialized.  That is normally done with setlocale:

           setlocale(LC_ALL, "");

 If the locale is not initialized, the library  assumes  that
 characters are printable as in ISO-8859-1, to work with cer-
 tain legacy programs.  You should initialize the locale  and
 not  rely on specific details of the library when the locale
 has not been setup.

针对后续评论,textpad.py在任何情况下都不会期望UTF-8输入。基本上,它会“验证”其输入,如果不是ASCII,则会忽略它。
Python的curses binding提供了一个接口到wgetch,它(与ncurses一起)为UTF-8提供单个字节。(X/Open Curses指定了一个不同的函数wget_wch,但Python没有相应的绑定)。 textpad.py可以通过将字节组装成Unicode值来修改以解决curses绑定的问题,但你需要将setlocale作为第一步。

1
我已经尝试过了,但是它并没有改变任何东西,上述的键仍然无法使用。 - lsgng

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接