/usr/share/doc/urwid-satext/examples/menu.py is in python-urwid-satext 0.2.0-5.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #!/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.keyHandler)
_frame.set_header(self.buildMenu())
_frame.set_focus('header')
def run(self):
self.loop.run()
def _messageExit(self, message):
#We print the menu data in the middle of the screen
new_widget = urwid.Filler(urwid.Text(message, align='center'))
self.loop.widget = new_widget
self.loop.draw_screen()
#5 seconds pause
time.sleep(5)
#see you
raise urwid.ExitMainLoop()
def menu_cb(self, menu_data):
self._messageExit("Menu selected: %s/%s" % menu_data)
def exit_cb(self, menu_data):
self._messageExit("Exiting throught 'Exit' menu item")
def buildMenu(self):
self.menu = Menu(self.loop)
_menu1 = "Menu 1"
self.menu.addMenu(_menu1, "Item 1", self.menu_cb) #Adding a menu is quite easy
self.menu.addMenu(_menu1, "Item 2", self.menu_cb) #Here the callback is always the same,
self.menu.addMenu(_menu1, "Item 3", self.menu_cb) #but you use different ones in real life :)
self.menu.addMenu(_menu1, "Exit (C-x)", self.exit_cb, 'ctrl x') #You can also add a shortcut
_menu2 = "Menu 2"
self.menu.addMenu(_menu2, "Item 1", self.menu_cb)
self.menu.addMenu(_menu2, "Item 2", self.menu_cb)
self.menu.addMenu(_menu2, "Item 3", self.menu_cb)
return self.menu
def keyHandler(self, input):
"""We leave if user press a quit char"""
if input in ('esc','q','Q'):
raise urwid.ExitMainLoop()
else:
return self.menu.checkShortcuts(input) #needed to manage shortcuts
demo = MenuDemo()
demo.run()
|