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()
can I get the item count directly from the ListBox?
This will give many items being selected
This will give the selected index and text