How do I use extended characters in Python's curses library?

mike picture mike · Aug 14, 2009 · Viewed 7.9k times · Source

I've been reading tutorials about Curses programming in Python, and many refer to an ability to use extended characters, such as line-drawing symbols. They're characters > 255, and the curses library knows how to display them in the current terminal font.

Some of the tutorials say you use it like this:

c = ACS_ULCORNER

...and some say you use it like this:

c = curses.ACS_ULCORNER

(That's supposed to be the upper-left corner of a box, like an L flipped vertically)

Anyway, regardless of which method I use, the name is not defined and the program thus fails. I tried "import curses" and "from curses import *", and neither works.

Curses' window() function makes use of these characters, so I even tried poking around on my box for the source to see how it does it, but I can't find it anywhere.

Answer

moeabdol picture moeabdol · Feb 10, 2015

you have to set your local to all, then encode your output as utf-8 as follows:

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()

output: あ