如何在 Python 的 curses 库中使用扩展字符?

mik*_*ike 4 python curses

我一直在阅读有关 Python Curses 编程的教程,其中许多教程都提到了使用扩展字符的能力,例如画线符号。它们是大于 255 的字符,curses 库知道如何以当前终端字体显示它们。

一些教程说你像这样使用它:

c = ACS_ULCORNER
Run Code Online (Sandbox Code Playgroud)

...有人说你是这样使用它的:

c = curses.ACS_ULCORNER
Run Code Online (Sandbox Code Playgroud)

(这应该是一个盒子的左上角,就像一个垂直翻转的L)

无论如何,无论我使用哪种方法,都没有定义名称,因此程序失败。我试过“导入诅咒”和“从诅咒导入*”,但都不起作用。

Curses 的 window() 函数使用了这些字符,所以我什至尝试在我的盒子上四处寻找源代码,看看是如何做到的,但我在任何地方都找不到它。

moe*_*dol 5

您必须将本地设置为全部,然后将您的输出编码为 utf-8,如下所示:

import curses
import locale

locale.setlocale(locale.LC_ALL, '')    # set your locale

scr = curses.initscr()
scr.clear()
scr.addstr(0, 0, u'\u3042'.encode('utf-8'))
scr.refresh()
# here implement simple code to wait for user input to quit
scr.endwin()
Run Code Online (Sandbox Code Playgroud)

输出: ?