python - urwid simple listbox example

5.4k Views Asked by At

I'm looking at urwid to develop a console app. As a starting point I'm looking for a simple example of a listbox that displays a few rows that can be scrolled using up/down keys.

Can anyone provide a simple example of how this can be done or point me to a link? I saw the examples on urwid site but I'm looking for something more basic.

Edit

@Adam: I found this example online. I'm having difficulties with the commented out part as I'm not familiar with the API and I'm a python newbie.

Edit2 I figured it out and updated the example. One more question - can I get the item count directly from the ListBox?

import urwid

palette = [('header', 'white', 'black'),
    ('reveal focus', 'black', 'dark cyan', 'standout')]

items = [urwid.Text("foo"),
         urwid.Text("bar"),
         urwid.Text("baz")]

content = urwid.SimpleListWalker([
    urwid.AttrMap(w, None, 'reveal focus') for w in items])

listbox = urwid.ListBox(content)

show_key = urwid.Text("Press any key", wrap='clip')
head = urwid.AttrMap(show_key, 'header')
top = urwid.Frame(listbox, head)

def show_all_input(input, raw):

    show_key.set_text("Pressed: " + " ".join([
        unicode(i) for i in input]))
    return input


def exit_on_cr(input):
    if input in ('q', 'Q'):
        raise urwid.ExitMainLoop()
    elif input == 'up':
        focus_widget, idx = listbox.get_focus()
        if idx > 0:
            idx = idx-1
            listbox.set_focus(idx)
    elif input == 'down':
        focus_widget, idx = listbox.get_focus()
        idx = idx+1
        listbox.set_focus(idx)

def out(s):
    show_key.set_text(str(s))


loop = urwid.MainLoop(top, palette,
    input_filter=show_all_input, unhandled_input=exit_on_cr)
loop.run()
2

There are 2 best solutions below

0
On

can I get the item count directly from the ListBox?

This will give many items being selected

listbox.get_focus_widgets()

This will give the selected index and text

listbox.focus_position
listbox.focus.base_widget.text
0
On

listboxes share their SimpleListWalker inside their body attribute which fortunately implements len correctly

len(listbox.body)

Further evidence :

(Pdb) listbox.body
SimpleListWalker([<AttrMap flow widget <Text flow widget 'Chemma!'> attr_map={None: None} focus_map={None: 'line'}>, <AttrMap flow widget <Divider flow widget div_char='-'> attr_map={None: None} focus_map={None: 'line'}>, <AttrMap flow widget <Text flow widget 'Another text widget!'> attr_map={None: None} focus_map={None: 'line'}>, <AttrMap flow widget <Divider flow widget div_char='-'> attr_map={None: None} focus_map={None: 'line'}>, <AttrMap flow widget <Text flow widget 'What is your name'> attr_map={None: None} focus_map={None: 'line'}>, <AttrMap flow widget <Divider flow widget div_char='-'> attr_map={None: None} focus_map={None: 'line'}>, <AttrMap flow widget <Text flow widget 'Boy ?'> attr_map={None: None} focus_map={None: 'line'}>])
(Pdb) len(listbox.body)
7
(Pdb)