diff options
| author | Ian Ward <ian@excess.org> | 2012-10-10 10:45:09 -0400 |
|---|---|---|
| committer | Ian Ward <ian@excess.org> | 2012-10-10 10:45:09 -0400 |
| commit | dca389bab9778d7b2b97459a02ac0ffdf2646613 (patch) | |
| tree | 8e44af437398296f18da580e4c48d4e296ba7b27 /docs/tutorial | |
| parent | f299155e39de97653e1a0c8a7c88bfb5f7b504e0 (diff) | |
| download | urwid-dca389bab9778d7b2b97459a02ac0ffdf2646613.tar.gz | |
tutorial: use exit buttons in menu examples
--HG--
branch : feature-sphinx
Diffstat (limited to 'docs/tutorial')
| -rw-r--r-- | docs/tutorial/index.rst | 24 | ||||
| -rw-r--r-- | docs/tutorial/menu1.py | 15 | ||||
| -rw-r--r-- | docs/tutorial/menu13.png | bin | 468 -> 561 bytes | |||
| -rw-r--r-- | docs/tutorial/menu2.py | 12 | ||||
| -rw-r--r-- | docs/tutorial/menu24.png | bin | 1344 -> 1418 bytes | |||
| -rw-r--r-- | docs/tutorial/menu3.py | 11 | ||||
| -rw-r--r-- | docs/tutorial/menu34.png | bin | 1233 -> 1637 bytes | |||
| -rw-r--r-- | docs/tutorial/menu4.py | 10 |
8 files changed, 22 insertions, 50 deletions
diff --git a/docs/tutorial/index.rst b/docs/tutorial/index.rst index 5d2081c..b0987bd 100644 --- a/docs/tutorial/index.rst +++ b/docs/tutorial/index.rst @@ -291,13 +291,7 @@ This program lets you choose an option then repeats what you chose. The buttons are decorated with an :class:`AttrMap` that applies a display attribute when a button is in focus. * *item_chosen* replaces the menu displayed with text indicating the users' - choice. It then installs *exit_program* as the *unhandled_input* method of the - :class:`MainLoop`. - - .. note:: This is a fairly benign example of monkey patching, but in - a real program an approach like the exit button in - the :ref:`signal handlers example <tutorial-signal-handlers>` above would be better. - + choice. * *exit_program* causes the program to exit on any keystroke. * The menu is created and decorated with an :class:`Overlay` using a :class:`SolidFill` as the background. The :class:`Overlay` is given a @@ -345,22 +339,6 @@ case as a base class for a widget that will be replacing its own contents regula to be removed and the previous one to be shown. This allows the user to return to a previous menu level. -.. note:: - In this example there is a subtle effect that prevents pressing *ESC* to return - to a menu once an item is chosen. - - *item_chosen* displays a widget that is not selectable, and the default - for :ref:`decoration widgets <decoration-widgets>` including :class:`WidgetPlaceholder` - is to reflect the selectable state of the widget contained. :class:`MainLoop` - will not call :meth:`Widget.keypress` - on non-selectable widgets, so *ESC* will not reach our *CascadingBoxes.keypress* - method once an item is chosen. - - If you did want to allow backing out after a selection was made you could make - *CascadingBoxes* selectable by defining a :meth:`Widget.selectable` method. - You would also need to handle exiting the program in a cleaner way than - by overwriting :meth:`MainLoop.unhandled_input` as *item_chosen* does above. - .. image:: menu21.png .. image:: menu22.png .. image:: menu23.png diff --git a/docs/tutorial/menu1.py b/docs/tutorial/menu1.py index 3b2c3c4..dea050e 100644 --- a/docs/tutorial/menu1.py +++ b/docs/tutorial/menu1.py @@ -11,12 +11,13 @@ def menu(title, choices): return urwid.ListBox(urwid.SimpleFocusListWalker(body)) def item_chosen(button, choice): - response = urwid.Text(u'You chose %s' % choice) - main.original_widget = urwid.Filler(response) - # exit on the next input from user - loop.unhandled_input = exit_program + response = urwid.Text([u'You chose ', choice, u'\n']) + done = urwid.Button(u'Ok') + urwid.connect_signal(done, 'click', exit_program) + main.original_widget = urwid.Filler(urwid.Pile([response, + urwid.AttrMap(done, None, focus_map='reversed')])) -def exit_program(key): +def exit_program(button): raise urwid.ExitMainLoop() main = urwid.Padding(menu(u'Pythons', choices), left=2, right=2) @@ -24,6 +25,4 @@ top = urwid.Overlay(main, urwid.SolidFill(u'\N{MEDIUM SHADE}'), 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() +urwid.MainLoop(top, palette=[('reversed', 'standout', '')]).run() diff --git a/docs/tutorial/menu13.png b/docs/tutorial/menu13.png Binary files differindex 67af851..775d841 100644 --- a/docs/tutorial/menu13.png +++ b/docs/tutorial/menu13.png diff --git a/docs/tutorial/menu2.py b/docs/tutorial/menu2.py index bc72ca6..fff0615 100644 --- a/docs/tutorial/menu2.py +++ b/docs/tutorial/menu2.py @@ -17,12 +17,11 @@ def menu(title, choices): return urwid.ListBox(urwid.SimpleFocusListWalker(body)) def item_chosen(button): - response = urwid.Text(u'You chose %s' % button.label) - top.open_box(urwid.Filler(response)) - # exit on the next input from user - loop.unhandled_input = exit_program + response = urwid.Text([u'You chose ', button.label, u'\n']) + done = menu_button(u'Ok', exit_program) + top.open_box(urwid.Filler(urwid.Pile([response, done]))) -def exit_program(key): +def exit_program(button): raise urwid.ExitMainLoop() menu_top = menu(u'Main Menu', [ @@ -65,5 +64,4 @@ class CascadingBoxes(urwid.WidgetPlaceholder): return super(CascadingBoxes, self).keypress(size, key) top = CascadingBoxes(menu_top) -loop = urwid.MainLoop(top, palette=[('reversed', 'standout', '')]) -loop.run() +urwid.MainLoop(top, palette=[('reversed', 'standout', '')]).run() diff --git a/docs/tutorial/menu24.png b/docs/tutorial/menu24.png Binary files differindex 0623baf..ef2b446 100644 --- a/docs/tutorial/menu24.png +++ b/docs/tutorial/menu24.png diff --git a/docs/tutorial/menu3.py b/docs/tutorial/menu3.py index 41198ba..eefedc9 100644 --- a/docs/tutorial/menu3.py +++ b/docs/tutorial/menu3.py @@ -32,10 +32,10 @@ class Choice(urwid.WidgetWrap): self.caption = caption def item_chosen(self, button): - response = urwid.Text(u'You chose %s' % self.caption) - top.open_box(urwid.Filler(response)) - # exit on the next input from user - loop.unhandled_input = exit_program + response = urwid.Text([u' You chose ', self.caption, u'\n']) + done = MenuButton(u'Ok', exit_program) + response_box = urwid.Filler(urwid.Pile([response, done])) + top.open_box(urwid.AttrMap(response_box, 'options')) def exit_program(key): raise urwid.ExitMainLoop() @@ -82,5 +82,4 @@ class HorizontalBoxes(urwid.Columns): top = HorizontalBoxes() top.open_box(menu_top) -loop = urwid.MainLoop(urwid.Filler(top, 'middle', 10), palette) -loop.run() +urwid.MainLoop(urwid.Filler(top, 'middle', 10), palette).run() diff --git a/docs/tutorial/menu34.png b/docs/tutorial/menu34.png Binary files differindex 39fc2ce..49b7122 100644 --- a/docs/tutorial/menu34.png +++ b/docs/tutorial/menu34.png diff --git a/docs/tutorial/menu4.py b/docs/tutorial/menu4.py index 35e0022..7d07688 100644 --- a/docs/tutorial/menu4.py +++ b/docs/tutorial/menu4.py @@ -33,13 +33,11 @@ class Thing(urwid.WidgetWrap): 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 + response = urwid.Text(u'You can make lemonade!\n') + done = ActionButton(u' - Joy', exit_program) + loop.widget = urwid.Filler(urwid.Pile([response, done])) -def exit_program(key): +def exit_program(button): raise urwid.ExitMainLoop() map_top = Place(u'porch', [ |
