Mercurial > urwid-satext
changeset 41:589e5e22f8f1
menu example: added shortcut example + some comments
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 20 Feb 2011 00:32:57 +0100 |
parents | 4da76342ae05 |
children | 585302e84c1f |
files | examples/menu.py |
diffstat | 1 files changed, 21 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/examples/menu.py Sun Feb 20 00:04:53 2011 +0100 +++ b/examples/menu.py Sun Feb 20 00:32:57 2011 +0100 @@ -18,16 +18,16 @@ def __init__(self): _frame = urwid.Frame(urwid.Filler(urwid.Text('Menu demo', align='center'))) - self.loop = urwid.MainLoop(_frame, const_PALETTE, unhandled_input=self.test_quit) + self.loop = urwid.MainLoop(_frame, const_PALETTE, unhandled_input=self.keyHandler) _frame.set_header(self.buildMenu()) _frame.set_focus('header') def run(self): self.loop.run() - def menu_cb(self, menu_data): + def _messageExit(self, message): #We print the menu data in the middle of the screen - new_widget = urwid.Filler(urwid.Text("Menu selected: %s/%s" % menu_data, align='center')) + new_widget = urwid.Filler(urwid.Text(message, align='center')) self.loop.widget = new_widget self.loop.draw_screen() #5 seconds pause @@ -35,22 +35,31 @@ #see you raise urwid.ExitMainLoop() + def menu_cb(self, menu_data): + self._messageExit("Menu selected: %s/%s" % menu_data) + + def exit_cb(self, menu_data): + self._messageExit("Exiting throught 'Exit' menu item") + def buildMenu(self): - _menu = Menu(self.loop) + self.menu = Menu(self.loop) _menu1 = "Menu 1" - _menu.addMenu(_menu1, "Item 1", self.menu_cb) - _menu.addMenu(_menu1, "Item 2", self.menu_cb) - _menu.addMenu(_menu1, "Item 3", self.menu_cb) + self.menu.addMenu(_menu1, "Item 1", self.menu_cb) #Adding a menu is quite easy + self.menu.addMenu(_menu1, "Item 2", self.menu_cb) #Here the callback is always the same, + self.menu.addMenu(_menu1, "Item 3", self.menu_cb) #but you use differents one in real life :) + self.menu.addMenu(_menu1, "Exit (C-x)", self.exit_cb, 'ctrl x') #You can also add a shortcut _menu2 = "Menu 2" - _menu.addMenu(_menu2, "Item 1", self.menu_cb) - _menu.addMenu(_menu2, "Item 2", self.menu_cb) - _menu.addMenu(_menu2, "Item 3", self.menu_cb) - return _menu + self.menu.addMenu(_menu2, "Item 1", self.menu_cb) + self.menu.addMenu(_menu2, "Item 2", self.menu_cb) + self.menu.addMenu(_menu2, "Item 3", self.menu_cb) + return self.menu - def test_quit(self, input): + def keyHandler(self, input): """We leave is user press 'esc'""" if input in ('esc','q','Q'): raise urwid.ExitMainLoop() + else: + return self.menu.checkShortcuts(input) demo = MenuDemo() demo.run()