Urwid Library

4.0.0 · active · verified Thu Apr 09

Urwid is a full-featured console user interface library for Python, allowing developers to create robust terminal applications with widgets, event loops, and rich display capabilities. The current version is 4.0.0. It maintains a regular release cadence with frequent bug fixes and minor improvements, alongside less frequent major version updates.

Warnings

Install

Imports

Quickstart

This quickstart initializes a basic Urwid application displaying text and a button. Clicking the button updates the display with a message, and pressing 'Q' at any point exits the application. It demonstrates key concepts like widgets (Text, Button, Pile, Filler), event handling (connect_signal), and the main event loop (MainLoop).

import urwid

def exit_on_q(key):
    if key in ('q', 'Q'):
        raise urwid.ExitMainLoop()

def show_message(button):
    response = urwid.Text([u"You clicked the button! ", ('bold', u"Press Q to quit.")])
    done = urwid.Button(u'Ok')
    urwid.connect_signal(done, 'click', exit_program)
    top_frame = urwid.Frame(
        header=urwid.Text("Message"),
        body=urwid.Filler(urwid.Pile([response, done]), 'top'),
        footer=None
    )
    loop.widget = top_frame # Replace current widget with message

def exit_program(button):
    raise urwid.ExitMainLoop()

# Create a text widget
text_widget = urwid.Text(u"Hello, Urwid! Press Space to click, Q to quit.")

# Create a button
button = urwid.Button(u'Click me!')
urwid.connect_signal(button, 'click', show_message)

# Arrange widgets in a pile, then fill the screen
pile = urwid.Pile([
    urwid.Text("Welcome to Urwid"),
    urwid.Divider(),
    text_widget,
    urwid.AttrMap(button, None, focus_map='reversed') # Add focus highlight
])
filler = urwid.Filler(pile, 'top')

# Create the main loop
loop = urwid.MainLoop(filler, unhandled_input=exit_on_q)
loop.run()

view raw JSON →