如何将 urwid.BigText 放入 urwid.ListBox

How to put a urwid.BigText into a urwid.ListBox

尝试在列表框顶部添加 BigText 时,我一直收到错误 AttributeError: 'BigText' object has no attribute 'rows'。我知道 BigText 是一个 "fixed" 小部件,而 ListBox 需要一个 "flow" 小部件,但无论我尝试什么,我似乎都无法让我的程序使用 BigText。这是我尝试过的详尽示例:

head_title = urwid.BigText(('banner', u'Header'), urwid.HalfBlock5x4Font())
head = urwid.Filler(head_title)
# head = urwid.AttrMap(head, 'banner')
# head = urwid.AttrMap(head, 'streak')
head = urwid.BoxAdapter(head, 3)
print head
# this gives me `<BoxAdapter flow widget <Filler box widget <BigText fixed widget>> height=3>`


body = [head, urwid.Divider()]
return urwid.ListBox(body)

谢谢!

BigText is a of 'fixed' sizing. That means that both the width and the height of the widget is defined by the widget. ListBox only accepts widgets of 'flow' sizing. This means that the width will be decided by the container (in this case the ListBox). So, you have to first convert he 'fixed' widget to a 'flow' widget. This can be done with the Padding装饰小部件通过设置宽度属性到'clip'。

完整示例请参见此处:

import urwid
def show_or_exit(key):
    if key in ('q', 'Q'):
        raise urwid.ExitMainLoop()
    return key

head = urwid.ListBox(urwid.SimpleFocusListWalker([
    urwid.Padding(
        urwid.BigText(('banner', "Hello world"), urwid.HalfBlock5x4Font()),
        width='clip')
]))

loop = urwid.MainLoop(head, unhandled_input=show_or_exit)
loop.run()