annotate examples/menu.py @ 84:9f683df69a4c

shortcut keys are now managed in separate module, with a class checking for conflicts
author Goffi <goffi@goffi.org>
date Thu, 04 Sep 2014 16:50:12 +0200
parents 287ff3e1edd1
children 6689aa54b20c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
40
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
3
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
4 import urwid
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
5 from urwid_satext.sat_widgets import Menu
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
6 import time
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
7
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
8
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
9 #These palette is optional, but it's easier to use with some colors :)
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
10
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
11 const_PALETTE = [('menubar', 'light gray,bold', 'dark red'),
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
12 ('menubar_focus', 'light gray,bold', 'dark green'),
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
13 ('menuitem', 'light gray,bold', 'dark red'),
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
14 ('menuitem_focus', 'light gray,bold', 'dark green'),
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
15 ]
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
16
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
17 class MenuDemo(object):
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
18
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
19 def __init__(self):
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
20 _frame = urwid.Frame(urwid.Filler(urwid.Text('Menu demo', align='center')))
41
589e5e22f8f1 menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents: 40
diff changeset
21 self.loop = urwid.MainLoop(_frame, const_PALETTE, unhandled_input=self.keyHandler)
40
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
22 _frame.set_header(self.buildMenu())
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
23 _frame.set_focus('header')
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 43
diff changeset
24
40
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
25 def run(self):
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
26 self.loop.run()
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
27
41
589e5e22f8f1 menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents: 40
diff changeset
28 def _messageExit(self, message):
40
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
29 #We print the menu data in the middle of the screen
41
589e5e22f8f1 menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents: 40
diff changeset
30 new_widget = urwid.Filler(urwid.Text(message, align='center'))
40
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
31 self.loop.widget = new_widget
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
32 self.loop.draw_screen()
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
33 #5 seconds pause
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
34 time.sleep(5)
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
35 #see you
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
36 raise urwid.ExitMainLoop()
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
37
41
589e5e22f8f1 menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents: 40
diff changeset
38 def menu_cb(self, menu_data):
589e5e22f8f1 menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents: 40
diff changeset
39 self._messageExit("Menu selected: %s/%s" % menu_data)
589e5e22f8f1 menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents: 40
diff changeset
40
589e5e22f8f1 menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents: 40
diff changeset
41 def exit_cb(self, menu_data):
589e5e22f8f1 menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents: 40
diff changeset
42 self._messageExit("Exiting throught 'Exit' menu item")
589e5e22f8f1 menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents: 40
diff changeset
43
40
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
44 def buildMenu(self):
41
589e5e22f8f1 menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents: 40
diff changeset
45 self.menu = Menu(self.loop)
40
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
46 _menu1 = "Menu 1"
41
589e5e22f8f1 menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents: 40
diff changeset
47 self.menu.addMenu(_menu1, "Item 1", self.menu_cb) #Adding a menu is quite easy
589e5e22f8f1 menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents: 40
diff changeset
48 self.menu.addMenu(_menu1, "Item 2", self.menu_cb) #Here the callback is always the same,
43
e49d8f289a73 menu example: ortho fix in comments :)
Goffi <goffi@goffi.org>
parents: 42
diff changeset
49 self.menu.addMenu(_menu1, "Item 3", self.menu_cb) #but you use different ones in real life :)
41
589e5e22f8f1 menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents: 40
diff changeset
50 self.menu.addMenu(_menu1, "Exit (C-x)", self.exit_cb, 'ctrl x') #You can also add a shortcut
40
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
51 _menu2 = "Menu 2"
41
589e5e22f8f1 menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents: 40
diff changeset
52 self.menu.addMenu(_menu2, "Item 1", self.menu_cb)
589e5e22f8f1 menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents: 40
diff changeset
53 self.menu.addMenu(_menu2, "Item 2", self.menu_cb)
589e5e22f8f1 menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents: 40
diff changeset
54 self.menu.addMenu(_menu2, "Item 3", self.menu_cb)
66
287ff3e1edd1 removed trailing spaces
Goffi <goffi@goffi.org>
parents: 43
diff changeset
55 return self.menu
40
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
56
41
589e5e22f8f1 menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents: 40
diff changeset
57 def keyHandler(self, input):
42
585302e84c1f fixed typo + added comment
Goffi <goffi@goffi.org>
parents: 41
diff changeset
58 """We leave if user press a quit char"""
40
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
59 if input in ('esc','q','Q'):
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
60 raise urwid.ExitMainLoop()
41
589e5e22f8f1 menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents: 40
diff changeset
61 else:
42
585302e84c1f fixed typo + added comment
Goffi <goffi@goffi.org>
parents: 41
diff changeset
62 return self.menu.checkShortcuts(input) #needed to manage shortcuts
40
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
63
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
64 demo = MenuDemo()
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
65 demo.run()