Urwid Readline

0.15.1 · active · verified Wed Apr 15

Urwid Readline (urwid-readline) is a Python library that provides a textbox edit widget for the Urwid console UI library, enhancing it with familiar readline-like keyboard shortcuts. Currently at version 0.15.1, the library maintains a stable release cadence as a specialized widget for Urwid applications.

Warnings

Install

Imports

Quickstart

This quickstart creates a simple Urwid application with a `ReadlineEdit` widget. It demonstrates basic input, modifying the widget's text on 'enter', and exiting the application by pressing 'q'. This provides a minimal but functional example of integrating `urwid-readline` into an Urwid application.

import urwid
from urwid_readline import ReadlineEdit

class MyReadlineEdit(ReadlineEdit):
    def keypress(self, size, key):
        if key == 'enter':
            # When 'enter' is pressed, update the widget's text
            self.set_edit_text(f'You typed: {self.edit_text}')
            return None # Indicate that the key was handled
        return super().keypress(size, key)


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

# Create an instance of our custom ReadlineEdit
edit = MyReadlineEdit(edit_text='Hello, Urwid Readline!', multiline=True)

# Wrap the edit widget in a Filler to allow it to expand
fill = urwid.Filler(edit, 'top')

# Set up the main loop with our fill widget and a handler for unhandled input
loop = urwid.MainLoop(fill, unhandled_input=exit_on_q)

# Run the Urwid application
loop.run()

view raw JSON →