diff options
| author | Ian Ward <ian@excess.org> | 2012-07-24 17:23:24 -0400 |
|---|---|---|
| committer | Ian Ward <ian@excess.org> | 2012-07-24 17:23:24 -0400 |
| commit | c9ee42570ac96a2697e4e2d4c51a93aee584cf42 (patch) | |
| tree | 92e348c77d66507616c461054ee74f42e7870131 /docs/tutorial | |
| parent | afa3d24d5ee1150da3a283abbf4270287e8beeaf (diff) | |
| download | urwid-c9ee42570ac96a2697e4e2d4c51a93aee584cf42.tar.gz | |
tutorial example: use Button subclass+signal instead of our own keypress methods
--HG--
branch : feature-sphinx
Diffstat (limited to 'docs/tutorial')
| -rw-r--r-- | docs/tutorial/menu.py | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/docs/tutorial/menu.py b/docs/tutorial/menu.py index a44015a..ed7c77c 100644 --- a/docs/tutorial/menu.py +++ b/docs/tutorial/menu.py @@ -3,31 +3,32 @@ import urwid inventory = set() loop = urwid.MainLoop(None) +class MenuButton(urwid.Button): + def __init__(self, text, callback): + super(MenuButton, self).__init__("", callback) + self._w = urwid.SelectableIcon(text, 1) + class SubMenu(urwid.WidgetWrap): def __init__(self, title, menu): super(SubMenu, self).__init__( - urwid.SelectableIcon(u" > go to " + title, 1)) + MenuButton(u" > go to " + title, self.pressed)) self.menu = menu - def keypress(self, size, key): - if key not in (' ', 'enter'): - return key + def pressed(self, button): loop.widget = self.menu class Thing(urwid.WidgetWrap): def __init__(self, name): super(Thing, self).__init__( - urwid.SelectableIcon(u" * take " + name, 1)) + MenuButton(u" * take " + name, self.pressed)) self.name = name - def keypress(self, size, key): - if key not in (' ', 'enter'): - return key + def pressed(self, button): self._w = urwid.Text(u" - " + self.name + " (taken)") inventory.add(self.name) if inventory >= set([u'sugar', u'lemon', u'jug']): raise urwid.ExitMainLoop() - return 'up' # move selection off this item + loop.process_input(["up"]) # move focus off this widget class Menu(urwid.ListBox): def __init__(self, title, children): |
