Python中的N-curses:如何捕获和打印非ASCII字符? [英] N-curses within Python : how to catch and print non ascii character?
问题描述
我想用ncurses/python创建一个小程序,并能够使用法文和日文输入/输入.我知道我应该设置语言环境并使用unicode标准.
I want to make a small program with ncurses/python and to be able to use/type in french and japanese. I understand that I should set the locale and use unicode standard.
但是如何处理screen.getch()的结果呢?无论语言如何,我都希望在ncurses窗口中显示键入的字符.
But how to deal with the result from screen.getch() ? I would like to display the typed character within the ncurses window regardless of the language.
我知道必须进行一些unicode转换,但是找不到做什么(我已经搜索了很多:对于业余爱好者来说,这种字符转换业务并不容易理解).
I understand that some unicode conversion is necessary but can't find what to do (and i've searched quite a bit : this character conversion bussiness isnt easy to understand for amateurs).
其他问题:似乎对于非ASCII字符,我们必须使用addstr()而不是addch().同样,我应该使用getstr()而不是getch()吗?
Additional question : it seems that for non ascii characters we must used addstr() instead of addch(). Similarly should I use getstr() instead of getch() ?
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import curses
from curses import wrapper
import locale
locale.setlocale(locale.LC_ALL, '')
def main(scr):
# Following lines are some sort of "proof of concept"
# Indeed it print latin or japanese characters allright
scr.addstr(0, 0, u'\u3042'.encode('utf-8')) # print あ
scr.addstr(1, 0, 'é'.encode('utf-8')) # print é
# But here I would like to type in a character and have it displayed onscreen
while (True):
car = scr.getch()
if car == 27: # = Escape key
break
else:
# What should I put between those parenthesis to
# print the typed character on the third line of the screen
scr.addstr(3, 0, ???? )
wrapper(main)
推荐答案
返回一个字符串,该字符串是字符ch的可打印表示形式.控制字符显示为插入符号,后跟该字符,例如,显示为 Return a string which is a printable representation of the character ch. Control characters are displayed as a caret followed by the character, for example as 如果您想直接阅读UTF-8,请使用 If you want to read UTF-8 directly, use 窗口. get_wch ([y,x]) window.get_wch([y, x]) 获得宽广的性格.对于大多数键,返回一个字符,对于功能键,键盘键和其他特殊键,返回一个整数.在无延迟模式下,如果没有输入,则引发异常. Get a wide character. Return a character for most keys, or an integer for function keys, keypad keys, and other special keys. In no-delay mode, raise an exception if there is no input. 版本 3.3 中的新功能. New in version 3.3. 即使使用 that ,您仍然必须确保语言环境已初始化. Python文档假定您可以访问ncurses文档: even with that, you still must ensure that the locale is initialized. The Python documentation assumes that you have access to the ncurses documentation: 这篇关于Python中的N-curses:如何捕获和打印非ASCII字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!unctrl
是要使用的函数,用于来自
curses.unctrl(
ch )
curses.unctrl(
ch)
^C
.保留打印字符.^C
. Printing characters are left as they are.get_wch
(在python2包装器中不可用):get_wch
(which was not available in the python2 wrapper):