Urwid

Table of Contents

Overview

Reference

ProgressBar

PALETTE = [
    ('normal', 'black', 'light gray'),
    ('complete', 'black', 'dark red'),
]

p = urwid.ProgressBar('normal', 'complete')

# Put the following line somewhere else
p.current += 1

Terminology

Topics

Display Attributes

palette = [
    ('banner', 'black', 'light gray'),
    ('streak', 'black', 'dark red'),
    ('bg', 'black', 'dark blue'),
]

urwid.MainLoop(fill, palette)

# While u"hello" text will appear with display attribute 'attr1',
# all surrounding whitespace will appear with display attribute 'attr2'.
# (gray area is 'attr1', red area is 'attr2')
AttrMap(Text(('attr1', u"Hello World")), 'attr2')

Widgets

How-to

Implement tick loop

def tick(loop, user_data):
    # TODO: Implement timed actions
    loop.set_alarm_in(0.1, tick, user_data)

Handle signals

import urwid

palette = [('I say', 'default,bold', 'default', 'bold'),]
ask = urwid.Edit(('I say', u"What is your name?\n"))
reply = urwid.Text(u"")
button = urwid.Button(u'Exit')
div = urwid.Divider()
pile = urwid.Pile([ask, div, reply, div, button])
top = urwid.Filler(pile, valign='top')

def on_ask_change(edit, new_edit_text):
    reply.set_text(('I say', u"Nice to meet you, %s" % new_edit_text))

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

urwid.connect_signal(ask, 'change', on_ask_change)
urwid.connect_signal(button, 'click', on_exit_clicked)

urwid.MainLoop(top, palette).run()

Links