diff options
| author | Ian Ward <ian@excess.org> | 2012-09-22 17:08:50 -0400 |
|---|---|---|
| committer | Ian Ward <ian@excess.org> | 2012-09-22 17:08:50 -0400 |
| commit | 9e73e7756f277a5edd6954043729d03fb47c8ca2 (patch) | |
| tree | bb811b09d1070b732dc01934d03e7f7924a8955e /docs/tutorial | |
| parent | 9a35396200f0bbce28dcbb2257285b4f7d014a8e (diff) | |
| download | urwid-9e73e7756f277a5edd6954043729d03fb47c8ca2.tar.gz | |
tutorial: make last menu example more interesting
--HG--
branch : feature-sphinx
Diffstat (limited to 'docs/tutorial')
| -rw-r--r-- | docs/tutorial/index.rst | 15 | ||||
| -rw-r--r-- | docs/tutorial/menu4.py | 72 | ||||
| -rw-r--r-- | docs/tutorial/menu4.py.xdotool | 2 | ||||
| -rw-r--r-- | docs/tutorial/menu41.png | bin | 696 -> 806 bytes | |||
| -rw-r--r-- | docs/tutorial/menu42.png | bin | 761 -> 1204 bytes | |||
| -rw-r--r-- | docs/tutorial/menu43.png | bin | 625 -> 1423 bytes | |||
| -rw-r--r-- | docs/tutorial/menu44.png | bin | 633 -> 1480 bytes |
7 files changed, 42 insertions, 47 deletions
diff --git a/docs/tutorial/index.rst b/docs/tutorial/index.rst index cebdd49..24de181 100644 --- a/docs/tutorial/index.rst +++ b/docs/tutorial/index.rst @@ -315,16 +315,13 @@ default decorations .. literalinclude:: menu3.py :linenos: -.. figure:: menu31.png +.. image:: menu31.png +.. image:: menu32.png +.. image:: menu33.png +.. image:: menu34.png -.. figure:: menu32.png - -.. figure:: menu33.png - -.. figure:: menu34.png - -"Adventure Game" in Menus -------------------------- +"Adventure Game" with ListBox History +------------------------------------- .. literalinclude:: menu4.py :linenos: diff --git a/docs/tutorial/menu4.py b/docs/tutorial/menu4.py index ee351e5..35e0022 100644 --- a/docs/tutorial/menu4.py +++ b/docs/tutorial/menu4.py @@ -2,45 +2,34 @@ import urwid inventory = set() -class MenuButton(urwid.Button): +class ActionButton(urwid.Button): def __init__(self, caption, callback): - super(MenuButton, self).__init__("") + super(ActionButton, 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) +class Place(urwid.WidgetWrap): + def __init__(self, name, choices): + super(Place, self).__init__( + ActionButton([u" > go to ", name], self.enter_place)) + self.heading = urwid.Text([u"\nLocation: ", name, "\n"]) + self.choices = choices # create links back to ourself for child in choices: - child_menu = getattr(child, 'menu', None) - if child_menu: - child_menu.set_parent(self) + getattr(child, 'choices', []).insert(0, 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] + def enter_place(self, button): + top.move_to(self) class Thing(urwid.WidgetWrap): def __init__(self, name): super(Thing, self).__init__( - MenuButton(u" * take " + name, self.take_thing)) + ActionButton([u" * take ", name], self.take_thing)) self.name = name def take_thing(self, button): + loop.process_input(["up"]) # move focus off this widget self._w = urwid.Text(u" - %s (taken)" % self.name) inventory.add(self.name) if inventory >= set([u'sugar', u'lemon', u'jug']): @@ -49,34 +38,43 @@ class Thing(urwid.WidgetWrap): # 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', [ +map_top = Place(u'porch', [ + Place(u'kitchen', [ + Place(u'refrigerator', []), + Place(u'cupboard', [ Thing(u'jug'), ]), ]), - SubMenu(u'garden', [ - SubMenu(u'tree', [ + Place(u'garden', [ + Place(u'tree', [ Thing(u'lemon'), Thing(u'bird'), ]), ]), - SubMenu(u'street', [ - SubMenu(u'store', [ + Place(u'street', [ + Place(u'store', [ Thing(u'sugar'), ]), - SubMenu(u'lake', [ - SubMenu(u'beach', []), + Place(u'lake', [ + Place(u'beach', []), ]), ]), ]) -loop = urwid.MainLoop(menu_top_sub.menu, - palette=[('reversed', 'standout', '')]) +class AdventureLog(urwid.ListBox): + def __init__(self): + log = urwid.SimpleFocusListWalker([]) + super(AdventureLog, self).__init__(log) + + def move_to(self, place): + self.body.append(urwid.Pile([place.heading] + place.choices)) + self.focus_position = len(self.body) - 1 + +top = AdventureLog() +top.move_to(map_top) +loop = urwid.MainLoop(top, palette=[('reversed', 'standout', '')]) loop.run() diff --git a/docs/tutorial/menu4.py.xdotool b/docs/tutorial/menu4.py.xdotool index 85999f6..a09d0fd 100644 --- a/docs/tutorial/menu4.py.xdotool +++ b/docs/tutorial/menu4.py.xdotool @@ -1,4 +1,4 @@ -windowsize --usehints $RXVTWINDOWID 23 8 +windowsize --usehints $RXVTWINDOWID 23 16 key --window $RXVTWINDOWID Return Down Down key --window $RXVTWINDOWID Return Down key --window $RXVTWINDOWID Return diff --git a/docs/tutorial/menu41.png b/docs/tutorial/menu41.png Binary files differindex 00b5a9f..60c36e0 100644 --- a/docs/tutorial/menu41.png +++ b/docs/tutorial/menu41.png diff --git a/docs/tutorial/menu42.png b/docs/tutorial/menu42.png Binary files differindex d7c0440..1aafd32 100644 --- a/docs/tutorial/menu42.png +++ b/docs/tutorial/menu42.png diff --git a/docs/tutorial/menu43.png b/docs/tutorial/menu43.png Binary files differindex 34f616a..ed8e6ef 100644 --- a/docs/tutorial/menu43.png +++ b/docs/tutorial/menu43.png diff --git a/docs/tutorial/menu44.png b/docs/tutorial/menu44.png Binary files differindex 3ff574f..2efacc7 100644 --- a/docs/tutorial/menu44.png +++ b/docs/tutorial/menu44.png |
