【问题标题】:Python curses - textpad.Textbox() keyboard input not working with German umlautsPython curses - textpad.Textbox() 键盘输入不适用于德语变音符号
【发布时间】:2017-07-19 12:48:18
【问题描述】:

我正在尝试使用 curses textpad.Textbox() 函数进行文本输入。到目前为止一切正常,但是,有些键无法识别,包括部分符号 (§) 和所有德语变音符号 (ä/ö/ü)。我想它与文本编码有某种关系,但我不知道如何解决这个问题。我的德语键盘布局与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()

【问题讨论】:

    标签: python encoding ncurses curses python-curses


    【解决方案1】:

    就像在 C 中一样,您应该初始化语言环境。 Python documentation:

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

    还有ncurses manual page

       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 作为第一步。

    【讨论】:

    • 我已经尝试过了,但它并没有改变任何东西,上述键仍然不起作用。
    猜你喜欢
    • 1970-01-01
    • 2011-11-07
    • 2013-02-09
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多