Exploring Python Text-Based UI Frameworks: Curses, npyscreen, and Urwid
This article introduces three popular Python terminal UI libraries—Curses, npyscreen, and Urwid—explaining their features, showing code examples, and highlighting how they enable sophisticated, interactive command‑line applications across different platforms.
Today we review three popular Python text‑based UI libraries: Curses, npyscreen, and Urwid.
Curses is a lightweight library that provides full‑screen text windows, supports colors, mouse, and function keys, and runs on any ANSI/POSIX‑compatible Unix/Linux system; on Windows it requires the windows-curses package.
Example usage:
import curses
myscreen = curses.initscr()
myscreen.border(0)
myscreen.addstr(12, 25, "Python curses in action!")
myscreen.refresh()
myscreen.getch()
curses.endwin()Key points: addstr uses character coordinates, getch blocks until a key is pressed, and curses.endwin() exits the window. For interactive programs a loop handling getch() is needed.
npyscreen builds on Curses to provide a higher‑level, widget‑based UI framework. It offers forms, text fields, date pickers, sliders, multi‑line editors, and selection widgets, automatically adapting to screen size.
Example usage:
import npyscreen
class TestApp(npyscreen.NPSApp):
def main(self):
F = npyscreen.Form(name="Welcome to Npyscreen")
F.add(npyscreen.TitleText, name="Text:")
F.add(npyscreen.TitleFilename, name="Filename:")
# ... additional widgets ...
F.edit()
print(F.get_selected_objects())
if __name__ == "__main__":
TestApp().run()Install with pip install npyscreen . The framework lets users navigate widgets with Tab/Shift‑Tab, confirm with Enter/Space, and supports multi‑selection.
Urwid is a more heavyweight, object‑oriented text UI library. It supports window resizing, automatic text alignment, a rich set of pre‑built widgets, multiple display modes, UTF‑8/CJK characters, and can integrate with event‑driven frameworks such as Twisted or Tornado.
Example usage:
import urwid
def show_or_exit(key):
if key in ('q', 'Q'):
raise urwid.ExitMainLoop()
txt.set_text(repr(key))
txt = urwid.Text("Hello World")
fill = urwid.Filler(txt, 'middle')
loop = urwid.MainLoop(fill, unhandled_input=show_or_exit)
loop.run()Urwid runs only on Linux; on Windows required components are missing.
These three libraries demonstrate that even in a non‑graphical environment you can build sophisticated, interactive command‑line applications, and they remain valuable for niche use‑cases such as terminal‑based music players.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.