2017-02-28 66 views
1

我试图使用curses textpad.Textbox()函数进行文本输入。到目前为止,一切正常,但是,某些键没有被识别,包括部分符号(§)和所有德语变音符号(ä/ö/ü)。我想这是与文本编码有关,但我不知道如何解决这个问题。我的德语键盘布局与input()完美配合。Python诅咒 - textpad.Textbox()键盘输入不与德国元音变音

下面是一些小例子:

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

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

+0

我已经试过,但它不会改变任何东西,前述的键仍然不起作用。 – lysigk