summaryrefslogtreecommitdiff
path: root/docs/tutorial/menu2.py
diff options
context:
space:
mode:
Diffstat (limited to 'docs/tutorial/menu2.py')
-rw-r--r--docs/tutorial/menu2.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/docs/tutorial/menu2.py b/docs/tutorial/menu2.py
new file mode 100644
index 0000000..aaf805d
--- /dev/null
+++ b/docs/tutorial/menu2.py
@@ -0,0 +1,46 @@
+import urwid
+
+def menu_button(caption, callback, data=None):
+ button = urwid.Button(caption)
+ urwid.connect_signal(button, 'click', callback, data)
+ return urwid.AttrMap(button, None, focus_map='reversed')
+
+def sub_menu(caption, choices):
+ contents = menu(caption, choices)
+ return menu_button(u'MENU: %s' % caption, open_menu, contents)
+
+def menu(title, choices):
+ body = [urwid.Text(title), urwid.Divider()]
+ body.extend(choices)
+ return urwid.ListBox(urwid.SimpleListWalker(body))
+
+def open_menu(button, menu):
+ loop.widget = menu
+
+def item_chosen(button):
+ response = urwid.Text(u'You chose %s' % button.label)
+ loop.widget = urwid.Filler(response)
+ # exit on the next input from user
+ loop.unhandled_input = exit_program
+
+def exit_program(key):
+ raise urwid.ExitMainLoop()
+
+menu_top = menu(u'Main Menu', [
+ sub_menu(u'Applications', [
+ sub_menu(u'Accessories', [
+ menu_button(u'Text Editor', item_chosen),
+ menu_button(u'Terminal', item_chosen),
+ ]),
+ ]),
+ sub_menu(u'System', [
+ sub_menu(u'Preferences', [
+ menu_button(u'Appearance', item_chosen),
+ ]),
+ menu_button(u'Lock Screen', item_chosen),
+ ]),
+])
+
+loop = urwid.MainLoop(menu_top,
+ palette=[('reversed', 'standout', '')])
+loop.run()