Search code examples
pythonlinuxconsoleurwid

I am unable to get urwid tutorial examples to work


Being new to both Python and urwid I was trying out the tutorial examples here http://excess.org/urwid/docs/tutorial/

However while the first ones do work fine, most of the later ones do not seem to work. For instance, trying to run this particular one:

import urwid

choices = u'Chapman Cleese Gilliam Idle Jones Palin'.split()

def menu(title, choices):
    body = [urwid.Text(title), urwid.Divider()]
    for c in choices:
        button = urwid.Button(c)
        urwid.connect_signal(button, 'click', item_chosen, c)
        body.append(urwid.AttrMap(button, None, focus_map='reversed'))
    return urwid.ListBox(urwid.SimpleFocusListWalker(body))

def item_chosen(button, choice):
    response = urwid.Text([u'You chose ', choice, u'\n'])
    done = urwid.Button(u'Ok')
    urwid.connect_signal(done, 'click', exit_program)
    main.original_widget = urwid.Filler(urwid.Pile([response,
        urwid.AttrMap(done, None, focus_map='reversed')]))

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

main = urwid.Padding(menu(u'Pythons', choices), left=2, right=2)
top = urwid.Overlay(main, urwid.SolidFill(u'\N{MEDIUM SHADE}'),
    align='center', width=('relative', 60),
    valign='middle', height=('relative', 60),
    min_width=20, min_height=9)
urwid.MainLoop(top, palette=[('reversed', 'standout', '')]).run()

Presents me with the following error:

Traceback (most recent call last):
  File "example.py", line 23, in <module>
    main = urwid.Padding(menu(u'Pythons', choices), left=2, right=2)
  File "example.py", line 11, in menu
    return urwid.ListBox(urwid.SimpleFocusListWalker(body))
AttributeError: 'module' object has no attribute 'SimpleFocusListWalker'

Am I doing something wrong? If it helps in any way I'm running Ubuntu 12.10 64bit.


Solution

  • I figured it out, I'm answering this myself just in case someone runs into the same problem.

    The root of this problem is that the tutorial examples use calls not present on earlier versions of urwid, like the one available on the Ubuntu repositories. Installing from source solves the problem.

    If you run into problems installing from source, make sure you have the python-dev package installed.