comparison 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
comparison
equal deleted inserted replaced
39:8cc5970039f3 40:4da76342ae05
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 import urwid
5 from urwid_satext.sat_widgets import Menu
6 import time
7
8
9 #These palette is optional, but it's easier to use with some colors :)
10
11 const_PALETTE = [('menubar', 'light gray,bold', 'dark red'),
12 ('menubar_focus', 'light gray,bold', 'dark green'),
13 ('menuitem', 'light gray,bold', 'dark red'),
14 ('menuitem_focus', 'light gray,bold', 'dark green'),
15 ]
16
17 class MenuDemo(object):
18
19 def __init__(self):
20 _frame = urwid.Frame(urwid.Filler(urwid.Text('Menu demo', align='center')))
21 self.loop = urwid.MainLoop(_frame, const_PALETTE, unhandled_input=self.test_quit)
22 _frame.set_header(self.buildMenu())
23 _frame.set_focus('header')
24
25 def run(self):
26 self.loop.run()
27
28 def menu_cb(self, menu_data):
29 #We print the menu data in the middle of the screen
30 new_widget = urwid.Filler(urwid.Text("Menu selected: %s/%s" % menu_data, align='center'))
31 self.loop.widget = new_widget
32 self.loop.draw_screen()
33 #5 seconds pause
34 time.sleep(5)
35 #see you
36 raise urwid.ExitMainLoop()
37
38 def buildMenu(self):
39 _menu = Menu(self.loop)
40 _menu1 = "Menu 1"
41 _menu.addMenu(_menu1, "Item 1", self.menu_cb)
42 _menu.addMenu(_menu1, "Item 2", self.menu_cb)
43 _menu.addMenu(_menu1, "Item 3", self.menu_cb)
44 _menu2 = "Menu 2"
45 _menu.addMenu(_menu2, "Item 1", self.menu_cb)
46 _menu.addMenu(_menu2, "Item 2", self.menu_cb)
47 _menu.addMenu(_menu2, "Item 3", self.menu_cb)
48 return _menu
49
50 def test_quit(self, input):
51 """We leave is user press 'esc'"""
52 if input in ('esc','q','Q'):
53 raise urwid.ExitMainLoop()
54
55 demo = MenuDemo()
56 demo.run()