Explore Python Terminal UI Frameworks: Curses, npyscreen, and Urwid
This article introduces three Python terminal UI libraries—Curses, npyscreen, and Urwid—explaining their features, showing sample code, and demonstrating how they enable interactive text‑based interfaces on Unix/Linux and, with extra steps, on Windows.
Curses
Curses is a dynamic library that provides text‑terminal window functionality. It can use the full screen, create and manage windows, display eight colors, support mouse, and handle function keys.
It runs on any ANSI/POSIX‑compliant Unix/Linux system; on Windows you need the windows-curses package: pip install windows-curses Example code:
import curses
myscreen = curses.initscr()
myscreen.border(0)
myscreen.addstr(12, 25, "Python curses in action!")
myscreen.refresh()
myscreen.getch()
curses.endwin()Note that addstr takes character coordinates, not pixel coordinates. getch blocks until a key is pressed. curses.endwin() exits the window.
To continuously listen for input you need a loop that processes getch() results.
Npyscreen
Npyscreen is a Python library built on top of Curses that offers a higher‑level UI framework with widgets such as Form, TitleText, TitleFilename, TitleDateCombo, TitleSlider, MultiLineEdit, TitleSelectOne, and TitleMultiSelect. It adapts to screen size.
Example:
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:")
fn2 = F.add(npyscreen.TitleFilenameCombo, name="Filename2:")
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!
Mutiline text, press ^R to reformat.", 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)
ms2 = F.add(npyscreen.TitleMultiSelect, max_height=-2, value=[1], name="Pick Several", values=["Option1","Option2","Option3"], scroll_exit=True)
F.edit()
print(ms.get_selected_objects())
if __name__ == "__main__":
App = TestApp()
App.run()Install with pip install npyscreen.
Subclass npyscreen.NPSApp to create an application.
Define a main method that builds a Form and adds widgets.
Call Form.edit() to hand control to the user.
Instantiate and run the app to start the UI loop.
Urwid
Urwid is a heavyweight text‑UI framework offering window auto‑resize, text alignment, easy block setting, powerful selection widgets, and integration with event‑driven libraries such as Twisted, Glib, and Tornado. It provides pre‑made widgets like edit boxes, buttons, and multi‑choice lists, supports native, Curses, LCD, and network displays, UTF‑8, CJK, and multiple colors.
Example:
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()Press Tab/Shift+Tab to move focus, Enter/Space to select.
Use hjkl keys for navigation similar to vim.
Note: Urwid runs only on Linux; it cannot run on Windows due to missing components.
Conclusion
The article introduced three popular Python terminal UI frameworks—Curses, Npyscreen, and Urwid—showing their capabilities and sample code. Although not mainstream, terminal UIs remain valuable in certain domains, and exploring them can reveal useful solutions such as the command‑line NetEase Music player built with Curses.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
