How to create nested listboxes in urwid?

醉酒当歌 提交于 2019-12-19 19:03:58

问题


Is it possible to put ListBoxes inside of SimpleListWalkers ? I'm trying to make nested ListBoxes, but I have this error :

AttributeError: 'MyListBox' object has no attribute 'rows'

import urwid

class MyListBox(urwid.ListBox):
    def focus_next(self):
        try: 
            self.body.set_focus(self.body.get_next(self.body.get_focus()[1])[1])
        except:
            pass
    def focus_previous(self):
        try: 
            self.body.set_focus(self.body.get_prev(self.body.get_focus()[1])[1])
        except:
            pass            

def handle_input(event):
    frame.header.set_text("key pressed %s" % event)
    if event == "q":
        raise urwid.ExitMainLoop
    elif event == "up":
        lb.focus_previous()
    elif event == "down" :
        lb.focus_next()        

widgets   = [urwid.AttrMap(urwid.Text(str(x)),None,"focus") for x in xrange(3)]
nested    = [urwid.AttrMap(urwid.Text(str(x)+"_sous"),None,"focus") for x in xrange(3)]
nested_lb = MyListBox(urwid.SimpleListWalker(nested))
lb        = MyListBox(urwid.SimpleListWalker(widgets+[nested_lb]))
frame     = urwid.Frame(lb,header=urwid.Text("Header"))
palette   = [("focus","dark cyan","white")]
loop      = urwid.MainLoop(frame,palette,unhandled_input = handle_input)
loop.screen.set_terminal_properties(colors=256)
loop.run()

回答1:


According to the manual ListBox is a box widget that contains flow widgets inside.

The difference between the types of widgets (box, flow and fixed) lies in the method of calculating their size. The details are described in the aforementioned link. In short: ListBox is informed about its size from its container, but requires its children to calculate their heights on their own. As another ListBox is inside it can't provide this value (has no rows method).

The solution is to wrap the inner ListBox in BoxAdapter that makes box widget to look and behave like flow widget:

...
widgets   = [urwid.AttrMap(urwid.Text(str(x)),None,"focus") for x in xrange(3)]
nested    = [urwid.AttrMap(urwid.Text(str(x)+"_sous"),None,"focus") for x in xrange(3)]
nested_lb = MyListBox(urwid.SimpleListWalker(nested))
lb        = MyListBox(urwid.SimpleListWalker(widgets+[urwid.BoxAdapter(nested_lb, 10)]))
...


来源:https://stackoverflow.com/questions/19500832/how-to-create-nested-listboxes-in-urwid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!