view 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
line wrap: on
line source

#!/usr/bin/python
# -*- coding: utf-8 -*-

import urwid
from urwid_satext.sat_widgets import Menu
import time


#These palette is optional, but it's easier to use with some colors :)

const_PALETTE = [('menubar', 'light gray,bold', 'dark red'),
                 ('menubar_focus', 'light gray,bold', 'dark green'),
                 ('menuitem', 'light gray,bold', 'dark red'),
                 ('menuitem_focus', 'light gray,bold', 'dark green'),
                 ]

class MenuDemo(object):

    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)
        _frame.set_header(self.buildMenu())
        _frame.set_focus('header')
    
    def run(self):
        self.loop.run()

    def menu_cb(self, menu_data):
        #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'))
        self.loop.widget = new_widget
        self.loop.draw_screen()
        #5 seconds pause
        time.sleep(5)
        #see you
        raise urwid.ExitMainLoop()

    def buildMenu(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)
        _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 

    def test_quit(self, input):
        """We leave is user press 'esc'"""
        if input in ('esc','q','Q'):
            raise urwid.ExitMainLoop()

demo = MenuDemo()
demo.run()