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 | |
| parent | da0f30e5561c8ed0b4a9300cab23d00283cec01c (diff) | |
| download | urwid-5df42e323c8bf3e8a4c2d880742fc9ee9b253b40.tar.gz | |
tutorial: menu3 modern columns-based look
--HG--
branch : feature-sphinx
Diffstat (limited to 'docs/tutorial')
| -rw-r--r-- | docs/tutorial/menu1.py | 4 | ||||
| -rw-r--r-- | docs/tutorial/menu2.py | 38 | ||||
| -rw-r--r-- | docs/tutorial/menu3.py | 55 |
3 files changed, 69 insertions, 28 deletions
diff --git a/docs/tutorial/menu1.py b/docs/tutorial/menu1.py index 98096f8..8925cf4 100644 --- a/docs/tutorial/menu1.py +++ b/docs/tutorial/menu1.py @@ -21,7 +21,9 @@ def exit_program(key): main = urwid.Padding(menu(u'Pythons', choices), left=2, right=2) top = urwid.Overlay(main, urwid.SolidFill(u'\u2592'), - 'center', 20, 'middle', 9) + align='center', width=('relative', 60), + valign='middle', height=('relative', 60), + min_width=20, min_height=9) loop = urwid.MainLoop(top, palette=[('reversed', 'standout', '')]) loop.run() diff --git a/docs/tutorial/menu2.py b/docs/tutorial/menu2.py index c158d6d..5ee7134 100644 --- a/docs/tutorial/menu2.py +++ b/docs/tutorial/menu2.py @@ -8,7 +8,7 @@ def menu_button(caption, callback): def sub_menu(caption, choices): contents = menu(caption, choices) def open_menu(button): - return top.open_menu(contents) + return top.open_box(contents) return menu_button(u'MENU: %s' % caption, open_menu) def menu(title, choices): @@ -18,7 +18,7 @@ def menu(title, choices): def item_chosen(button): response = urwid.Text(u'You chose %s' % button.label) - top.open_menu(urwid.Filler(response)) + top.open_box(urwid.Filler(response)) # exit on the next input from user loop.unhandled_input = exit_program @@ -40,18 +40,30 @@ menu_top = menu(u'Main Menu', [ ]), ]) -class NestedMenus(urwid.WidgetPlaceholder): - def __init__(self, menu): - super(NestedMenus, self).__init__(urwid.SolidFill(u'/')) - self.menu_level = 0 - self.open_menu(menu) +class CascadingBoxes(urwid.WidgetPlaceholder): + def __init__(self, box): + super(CascadingBoxes, self).__init__(urwid.SolidFill(u'/')) + self.box_level = 0 + self.open_box(box) - def open_menu(self, menu): - self.original_widget = urwid.Overlay(urwid.LineBox(menu), - self.original_widget, 'left', 24, 'top', 8, - left=self.menu_level * 2, top=self.menu_level * 2) - self.menu_level += 1 + def open_box(self, box): + # NOTE: assumes we'll only ever have 4 boxes + self.original_widget = urwid.Overlay(urwid.LineBox(box), + self.original_widget, + align='center', width=('relative', 80), + valign='middle', height=('relative', 80), + min_width=24, min_height=8, + left=self.box_level * 3, right=(3 - self.box_level) * 3, + top=self.box_level * 2, bottom=(3 - self.box_level) * 2) + self.box_level += 1 -top = NestedMenus(menu_top) + def keypress(self, size, key): + if key == 'esc' and self.box_level > 1: + self.original_widget = self.original_widget[0] + self.box_level -= 1 + else: + return super(CascadingBoxes, self).keypress(size, key) + +top = CascadingBoxes(menu_top) loop = urwid.MainLoop(top, palette=[('reversed', 'standout', '')]) loop.run() 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() |
