Mercurial > urwid-satext
changeset 40:4da76342ae05
added Menu example
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 20 Feb 2011 00:04:53 +0100 |
parents | 8cc5970039f3 |
children | 589e5e22f8f1 |
files | examples/file_dialog.py examples/menu.py |
diffstat | 2 files changed, 57 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/examples/file_dialog.py Tue Jan 11 15:32:14 2011 +0100 +++ b/examples/file_dialog.py Sun Feb 20 00:04:53 2011 +0100 @@ -34,7 +34,7 @@ def test_quit(input): """We leave is user press 'esc'""" - if input in ('esc'): + if input in ('esc',): raise urwid.ExitMainLoop() fd = FileDialog(ok_cb, cancel_cb)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/menu.py Sun Feb 20 00:04:53 2011 +0100 @@ -0,0 +1,56 @@ +#!/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()