annotate examples/menu.py @ 40:4da76342ae05

added Menu example
author Goffi <goffi@goffi.org>
date Sun, 20 Feb 2011 00:04:53 +0100
parents
children 589e5e22f8f1
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')))
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
21 self.loop = urwid.MainLoop(_frame, const_PALETTE, unhandled_input=self.test_quit)
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')
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
24
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
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
28 def menu_cb(self, menu_data):
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
29 #We print the menu data in the middle of the screen
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
30 new_widget = urwid.Filler(urwid.Text("Menu selected: %s/%s" % menu_data, align='center'))
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
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
38 def buildMenu(self):
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
39 _menu = Menu(self.loop)
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
40 _menu1 = "Menu 1"
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
41 _menu.addMenu(_menu1, "Item 1", self.menu_cb)
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
42 _menu.addMenu(_menu1, "Item 2", self.menu_cb)
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
43 _menu.addMenu(_menu1, "Item 3", self.menu_cb)
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
44 _menu2 = "Menu 2"
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
45 _menu.addMenu(_menu2, "Item 1", self.menu_cb)
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
46 _menu.addMenu(_menu2, "Item 2", self.menu_cb)
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
47 _menu.addMenu(_menu2, "Item 3", self.menu_cb)
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
48 return _menu
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
49
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
50 def test_quit(self, input):
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
51 """We leave is user press 'esc'"""
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
52 if input in ('esc','q','Q'):
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
53 raise urwid.ExitMainLoop()
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
54
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
55 demo = MenuDemo()
4da76342ae05 added Menu example
Goffi <goffi@goffi.org>
parents:
diff changeset
56 demo.run()