3

Is it possible to track the change of the highlighted item in an urwid.ListBox object? Or even via a ListWalker object?

I would like to call a callback when the user moves from one item to another using the arrow keys [], [], not when the user hits [Enter] on one item.

enter image description here

imrek
  • 2,930
  • 3
  • 20
  • 36

1 Answers1

7

After some research and experimentation, it's possible to do this by registering the modified signal with the ListWalker object.

import urwid

def callback():
    index = str(listBox.get_focus()[1])
    debug.set_text("Index of selected item: " + index)


debug = urwid.Text("Debug")

captions = "A B C D E F".split()
items = [urwid.Button(caption) for caption in captions]
walker = urwid.SimpleListWalker(items)
listBox = urwid.ListBox(walker)

urwid.connect_signal(walker, "modified", callback)

frame = urwid.Frame(body=listBox, header=debug)
urwid.MainLoop(frame).run()

Reference: Urwid > Signal functions > connect

enter image description here

imrek
  • 2,930
  • 3
  • 20
  • 36