Is there a focus changed event in urwid?

Can you track the change of a selected item in an object urwid.ListBox

? Or even through an object ListWalker

?

I would like to call a callback when the user moves from one element to another using the arrow keys [πŸ ‰]

, [πŸ ‹]

rather than when a user clicks [Enter]

on one element.

enter image description here

+3


source to share


1 answer


After some research and experimentation, this can be done by registering the signal modified

with an object ListWalker

.

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()

      



Ref: Urwid> Signal Functions> connect

enter image description here

+4


source







All Articles