Urwid Library
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
- breaking Version 4.0.0 removed several public and protected methods that were previously deprecated. Examples include `Filler.get_body()`, `Filler.set_body()`, and various methods from `Canvas` and `AttrSpec`.
- breaking Version 3.0.0 removed several legacy widgets and properties, including `FlowWidget`, `BoxWidget`, `FixedWidget`, and the `__super` property. Code relying on these will fail.
- gotcha Windows support for `urwid` is primarily for Windows 10+ and relies on the default console host (conhost). There may be visual quirks, performance issues, or incomplete feature support (e.g., mouse events) compared to Linux/macOS terminals.
- gotcha While `user_arg` for `urwid.connect_signal` was briefly deprecated (v3.0.4) and then un-deprecated (v3.0.5), it can still lead to confusion. The `user_args` parameter (as a tuple) is generally preferred for passing multiple arguments or for consistency with other callback patterns.
- gotcha Urwid's main loop typically captures all input, including mouse events and keyboard input. This can make debugging or external interaction with the terminal challenging. Ensure your `unhandled_input` handler or event filtering is robust.
Install
-
pip install urwid
Imports
- urwid
import urwid
- Text
import urwid urwid.Text(...)
- MainLoop
import urwid urwid.MainLoop(...)
- Button
import urwid urwid.Button(...)
Quickstart
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()