【发布时间】:2019-10-15 19:42:24
【问题描述】:
我想用 ncurses/python 制作一个小程序,并能够使用/输入法文和日文。我知道我应该设置语言环境并使用 unicode 标准。
但是如何处理 screen.getch() 的结果呢?无论语言如何,我都想在 ncurses 窗口中显示键入的字符。
我知道一些 unicode 转换是必要的,但找不到该怎么做(我已经搜索了很多:这种字符转换业务对于业余爱好者来说并不容易理解)。
附加问题:似乎对于非 ascii 字符,我们必须使用 addstr() 而不是 addch()。同样,我应该使用 getstr() 而不是 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)
【问题讨论】:
-
看起来您使用的是 Linux 或其他类 Unix(不是 Windows)。能否确认一下,能否确认不需要Windows兼容?
-
确实我使用的是 linux(基于 debian 的发行版,称为 bunsen labs),我不需要 windows 兼容性。
标签: python unicode ncurses non-ascii-characters python-curses