diff options
| author | Ian Ward <ian@excess.org> | 2012-09-21 14:46:10 -0400 |
|---|---|---|
| committer | Ian Ward <ian@excess.org> | 2012-09-21 14:46:10 -0400 |
| commit | 5df42e323c8bf3e8a4c2d880742fc9ee9b253b40 (patch) | |
| tree | c8c789019dfd4b9c49980807508e187a2e708071 /docs/tutorial/menu3.py | |
| parent | da0f30e5561c8ed0b4a9300cab23d00283cec01c (diff) | |
| download | urwid-5df42e323c8bf3e8a4c2d880742fc9ee9b253b40.tar.gz | |
tutorial: menu3 modern columns-based look
--HG--
branch : feature-sphinx
Diffstat (limited to 'docs/tutorial/menu3.py')
| -rw-r--r-- | docs/tutorial/menu3.py | 55 |
1 files changed, 41 insertions, 14 deletions
diff --git a/docs/tutorial/menu3.py b/docs/tutorial/menu3.py index e3bd0bb..f6e3a9d 100644 --- a/docs/tutorial/menu3.py +++ b/docs/tutorial/menu3.py @@ -4,25 +4,26 @@ class MenuButton(urwid.Button): def __init__(self, caption, callback): super(MenuButton, self).__init__("") urwid.connect_signal(self, 'click', callback) - self._w = urwid.AttrMap(urwid.SelectableIcon(caption, 0), - None, focus_map='reversed') + self._w = urwid.AttrMap(urwid.SelectableIcon( + [u' \N{BULLET} ', caption], 2), None, 'selected') class SubMenu(urwid.WidgetWrap): def __init__(self, caption, choices): - super(SubMenu, self).__init__( - MenuButton(u"MENU: %s" % caption, self.open_menu)) + super(SubMenu, self).__init__(MenuButton( + [caption, u"\N{HORIZONTAL ELLIPSIS}"], self.open_menu)) self.menu = Menu(caption, choices) def open_menu(self, button): - loop.widget = self.menu + top.open_box(self.menu) -class Menu(urwid.ListBox): +class Menu(urwid.WidgetWrap): def __init__(self, title, choices): - super(Menu, self).__init__(urwid.SimpleListWalker([ - urwid.Text(title), - urwid.Divider()])) - self.body.extend(choices) - self.title = title + line = urwid.AttrMap(urwid.Divider(u'\N{LOWER ONE QUARTER BLOCK}'), + 'line') + listbox = urwid.ListBox(urwid.SimpleFocusListWalker([ + urwid.AttrMap(urwid.Text([u"\n ", title]), 'heading'), + line, urwid.Divider()] + choices + [urwid.Divider()])) + super(Menu, self).__init__(urwid.AttrMap(listbox, 'options')) class Choice(urwid.WidgetWrap): def __init__(self, caption): @@ -32,7 +33,7 @@ class Choice(urwid.WidgetWrap): def item_chosen(self, button): response = urwid.Text(u'You chose %s' % self.caption) - loop.widget = urwid.Filler(response) + top.open_box(urwid.Filler(response)) # exit on the next input from user loop.unhandled_input = exit_program @@ -54,6 +55,32 @@ menu_top = Menu(u'Main Menu', [ ]), ]) -loop = urwid.MainLoop(menu_top, - palette=[('reversed', 'standout', '')]) +palette = [ + (None, 'light gray', 'black'), + ('heading', 'black', 'light gray'), + ('line', 'black', 'light gray'), + ('options', 'dark gray', 'black'), + ('focus heading', 'white', 'dark red'), + ('focus line', 'black', 'dark red'), + ('focus options', 'black', 'light gray'), + ('selected', 'white', 'dark blue')] +focus_map = { + 'heading': 'focus heading', + 'options': 'focus options', + 'line': 'focus line'} + +class HorizontalBoxes(urwid.Columns): + def __init__(self): + super(HorizontalBoxes, self).__init__([], dividechars=1) + + def open_box(self, box): + if self.contents: + del self.contents[self.focus_position + 1:] + self.contents.append((urwid.AttrMap(box, 'options', focus_map), + self.options('given', 24))) + self.focus_position = len(self.contents) - 1 + +top = HorizontalBoxes() +top.open_box(menu_top) +loop = urwid.MainLoop(top, palette) loop.run() |
