diff options
| author | Ian Ward <ian@excess.org> | 2012-08-16 11:49:57 -0400 |
|---|---|---|
| committer | Ian Ward <ian@excess.org> | 2012-08-16 11:49:57 -0400 |
| commit | d2c5b6893ed9f56308fedf90e7ce2ff0f65eea8c (patch) | |
| tree | 49a2c95ca1a4c146ebb16be739dd24edc31a5711 /docs/tutorial | |
| parent | e805b9722ca07d9f66ce6a162e126a59fdc310fd (diff) | |
| download | urwid-d2c5b6893ed9f56308fedf90e7ce2ff0f65eea8c.tar.gz | |
tutorial example: menus with parent links
--HG--
branch : feature-sphinx
Diffstat (limited to 'docs/tutorial')
| -rw-r--r-- | docs/tutorial/menu.py | 80 | ||||
| -rw-r--r-- | docs/tutorial/menu4.py | 82 |
2 files changed, 82 insertions, 80 deletions
diff --git a/docs/tutorial/menu.py b/docs/tutorial/menu.py deleted file mode 100644 index b716992..0000000 --- a/docs/tutorial/menu.py +++ /dev/null @@ -1,80 +0,0 @@ -import urwid - -inventory = set() - -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, 1), - None, focus_map='reversed') - -class SubMenu(urwid.WidgetWrap): - def __init__(self, title, menu): - super(SubMenu, self).__init__( - MenuButton(u" > go to " + title, self.clicked)) - self.menu = menu - - def clicked(self): - loop.widget = self.menu - -class Thing(urwid.WidgetWrap): - def __init__(self, name): - super(Thing, self).__init__( - MenuButton(u" * take " + name, self.clicked)) - self.name = name - - def clicked(self): - self._w = urwid.Text(u" - " + self.name + " (taken)") - inventory.add(self.name) - if inventory >= set([u'sugar', u'lemon', u'jug']): - raise urwid.ExitMainLoop() - loop.process_input(["up"]) # move focus off this widget - -class Menu(urwid.ListBox): - def __init__(self, title, children): - super(Menu, self).__init__(urwid.SimpleListWalker([ - urwid.Text(u"Location: " + title), - urwid.Divider()] + list(children))) - self.title = title - - def set_parent(self, sub_menu): - self.body[2:2] = [sub_menu] - -def menu(title, *contents): - """Create menus and links back to parent menus""" - menu_full = Menu(title, contents) - sub_menu = SubMenu(title, menu_full) - for child in contents: - child_menu = getattr(child, 'menu', None) - if child_menu: - child_menu.set_parent(sub_menu) - return sub_menu - -menu_top = menu(u'porch', - menu(u'kitchen', - menu(u'refrigerator'), - menu(u'cupboard', - Thing(u'jug'), - ), - ), - menu(u'garden', - menu(u'tree', - Thing(u'lemon'), - Thing(u'bird'), - ), - ), - menu(u'street', - menu(u'store', - Thing(u'sugar'), - ), - menu(u'lake', - menu(u'beach'), - ), - ), - ) - -loop = urwid.MainLoop(menu_top.menu, - palette=[('reversed', 'standout', '')]) -loop.run() -print u"Congratulations, you can make lemonade!" diff --git a/docs/tutorial/menu4.py b/docs/tutorial/menu4.py new file mode 100644 index 0000000..ee351e5 --- /dev/null +++ b/docs/tutorial/menu4.py @@ -0,0 +1,82 @@ +import urwid + +inventory = set() + +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, 1), + None, focus_map='reversed') + +class SubMenu(urwid.WidgetWrap): + def __init__(self, caption, choices): + super(SubMenu, self).__init__( + MenuButton(u" > go to " + caption, self.open_menu)) + self.menu = Menu(caption, choices) + # create links back to ourself + for child in choices: + child_menu = getattr(child, 'menu', None) + if child_menu: + child_menu.set_parent(self) + + def open_menu(self, button): + loop.widget = self.menu + +class Menu(urwid.ListBox): + def __init__(self, title, choices): + super(Menu, self).__init__(urwid.SimpleListWalker([ + urwid.Text(u"Location: " + title), + urwid.Divider()])) + self.body.extend(choices) + self.title = title + + def set_parent(self, sub_menu): + self.body[2:2] = [sub_menu] + +class Thing(urwid.WidgetWrap): + def __init__(self, name): + super(Thing, self).__init__( + MenuButton(u" * take " + name, self.take_thing)) + self.name = name + + def take_thing(self, button): + self._w = urwid.Text(u" - %s (taken)" % self.name) + inventory.add(self.name) + if inventory >= set([u'sugar', u'lemon', u'jug']): + response = urwid.Text(u'You can make lemonade!') + loop.widget = urwid.Filler(response) + # exit on the next input from user + loop.unhandled_input = exit_program + return + loop.process_input(["up"]) # move focus off this widget + +def exit_program(key): + raise urwid.ExitMainLoop() + +menu_top_sub = SubMenu(u'porch', [ + SubMenu(u'kitchen', [ + SubMenu(u'refrigerator', []), + SubMenu(u'cupboard', [ + Thing(u'jug'), + ]), + ]), + SubMenu(u'garden', [ + SubMenu(u'tree', [ + Thing(u'lemon'), + Thing(u'bird'), + ]), + ]), + SubMenu(u'street', [ + SubMenu(u'store', [ + Thing(u'sugar'), + ]), + SubMenu(u'lake', [ + SubMenu(u'beach', []), + ]), + ]), +]) + +loop = urwid.MainLoop(menu_top_sub.menu, + palette=[('reversed', 'standout', '')]) +loop.run() |
