Fundamentals 8 min read

Introduction to Python Text‑Based UI Frameworks: Curses, npyscreen, and Urwid

This article introduces three popular Python text‑based UI frameworks—Curses, npyscreen, and Urwid—explaining their features, installation steps, and providing runnable code examples that demonstrate creating windows, forms, and interactive command‑line applications for developers seeking lightweight terminal interfaces.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Introduction to Python Text‑Based UI Frameworks: Curses, npyscreen, and Urwid

This article surveys three widely used Python libraries for building text‑based user interfaces in a terminal: the low‑level Curses library, the higher‑level npyscreen framework, and the feature‑rich Urwid toolkit.

Curses provides basic window management, color support, mouse handling, and keyboard function keys on any ANSI/POSIX‑compatible Unix/Linux system. On Windows the additional pip install windows-curses package is required. A minimal example creates a screen, draws a border, prints a message, and waits for a key press:

<code>import curses
myscreen = curses.initscr()
myscreen.border(0)
myscreen.addstr(12, 25, "Python curses in action!")
myscreen.refresh()
myscreen.getch()
curses.endwin()
</code>

Key points: addstr uses character coordinates, getch blocks until input, and curses.endwin() restores the terminal.

npyscreen builds on Curses to offer a UI‑style programming model with ready‑made widgets such as Form , TitleText , TitleDateCombo , MultiLineEdit , TitleSelectOne , and TitleSlider . The following script defines a simple application that assembles a form with several controls and prints the selected options after the user exits:

<code>import npyscreen

class TestApp(npyscreen.NPSApp):
    def main(self):
        F = npyscreen.Form(name="Welcome to Npyscreen")
        t = F.add(npyscreen.TitleText, name="Text:")
        fn = F.add(npyscreen.TitleFilename, name="Filename:")
        dt = F.add(npyscreen.TitleDateCombo, name="Date:")
        s = F.add(npyscreen.TitleSlider, out_of=12, name="Slider")
        ml = F.add(npyscreen.MultiLineEdit, value="try typing here!", max_height=5, rely=9)
        ms = F.add(npyscreen.TitleSelectOne, max_height=4, value=[1], name="Pick One", values=["Option1","Option2","Option3"], scroll_exit=True)
        F.edit()
        print(ms.get_selected_objects())

if __name__ == "__main__":
    App = TestApp()
    App.run()
</code>

Important notes: install with pip install npyscreen , inherit from npyscreen.NPSApp , create widgets via Form.add() , and call Form.edit() to start the interactive loop.

Urwid is a more heavyweight, object‑oriented framework that supports adaptive layouts, automatic text alignment, rich widget sets, and integration with event‑driven libraries such as Twisted or Tornado. A concise example creates a centered text widget that displays the pressed key and exits on q or Q :

<code>import urwid

def show_or_exit(key):
    if key in ('q', 'Q'):
        raise urwid.ExitMainLoop()
    txt.set_text(repr(key))

txt = urwid.Text(u"Hello World")
fill = urwid.Filler(txt, 'middle')
loop = urwid.MainLoop(fill, unhandled_input=show_or_exit)
loop.run()
</code>

Urwid runs only on Linux because it depends on native Curses components.

The article concludes that, although terminal UI frameworks are no longer mainstream, they remain valuable for specialized tools, quick prototyping, and environments where graphical interfaces are unavailable.

CLIPythoncursesTerminal UInpyscreenurwid
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.