Python诅咒-textpad.Textbox()键盘输入不适用于德语变音符号 [英] Python curses - textpad.Textbox() keyboard input not working with German umlauts

查看:156
本文介绍了Python诅咒-textpad.Textbox()键盘输入不适用于德语变音符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用curses textpad.Textbox()函数进行文本输入.到目前为止,一切都工作正常,但是,某些键仍未被识别,包括区号(§)和所有德国变音符号(ä/ö/ü).我想这与文本编码有关,但是我不知道如何解决.我的德语键盘布局与input()完美搭配.

I'm trying to use the curses textpad.Textbox() function for text input. Everything is working fine so far, however, some keys don't get recognized, including the section sign (§) and all German umlauts (ä/ö/ü). I guess it's somehow related to the text encoding, but I have no idea how to fix this. My German keyboard layout works perfectly fine with 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()

推荐答案

就像在C语言中一样,您应该初始化语言环境.在 Python文档中说明:

Just as in C, you should initialize the locale. It's spelled out in both the Python documentation:

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

Since version 5.4, the ncurses library decides how to interpret non-ASCII data using the nl_langinfo function. That means that you have to call locale.setlocale() in the application and encode Unicode strings using one of the system’s available encodings.

ncurses手册页:

   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,否则将忽略它.

Addressing the followup comment, textpad.py does not expect UTF-8 input in any case. Essentially it "validates" its input, decides it isn't ASCII and ignores it when it's not.

Python的诅咒绑定提供了一个接口 wgetch ,其中(带有 ncurses )给出了UTF-8的各个字节. (X/Open诅咒为 wget_wch 指定了不同的功能哪个Python没有绑定).

Python's curses binding provides an interface to wgetch, which (with ncurses) gives the individual bytes for the UTF-8. (X/Open Curses specifies a different function wget_wch, for which Python has no binding).

textpad.py修改为通过将字节组装成Unicode值来解决curses绑定,但是第一步需要setlocale.

textpad.py could be modified to work around the curses binding by assembling the bytes into a Unicode value, but you'd need the setlocale as the first step.

这篇关于Python诅咒-textpad.Textbox()键盘输入不适用于德语变音符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆