Mercurial > urwid-satext
annotate examples/menu.py @ 144:bfab04d0a745
ListOption: add __hash__ method
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 27 Oct 2019 18:55:47 +0100 |
parents | 287ff3e1edd1 |
children | 6689aa54b20c |
rev | line source |
---|---|
40 | 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'))) | |
41
589e5e22f8f1
menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents:
40
diff
changeset
|
21 self.loop = urwid.MainLoop(_frame, const_PALETTE, unhandled_input=self.keyHandler) |
40 | 22 _frame.set_header(self.buildMenu()) |
23 _frame.set_focus('header') | |
66 | 24 |
40 | 25 def run(self): |
26 self.loop.run() | |
27 | |
41
589e5e22f8f1
menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents:
40
diff
changeset
|
28 def _messageExit(self, message): |
40 | 29 #We print the menu data in the middle of the screen |
41
589e5e22f8f1
menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents:
40
diff
changeset
|
30 new_widget = urwid.Filler(urwid.Text(message, align='center')) |
40 | 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 | |
41
589e5e22f8f1
menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents:
40
diff
changeset
|
38 def menu_cb(self, menu_data): |
589e5e22f8f1
menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents:
40
diff
changeset
|
39 self._messageExit("Menu selected: %s/%s" % menu_data) |
589e5e22f8f1
menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents:
40
diff
changeset
|
40 |
589e5e22f8f1
menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents:
40
diff
changeset
|
41 def exit_cb(self, menu_data): |
589e5e22f8f1
menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents:
40
diff
changeset
|
42 self._messageExit("Exiting throught 'Exit' menu item") |
589e5e22f8f1
menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents:
40
diff
changeset
|
43 |
40 | 44 def buildMenu(self): |
41
589e5e22f8f1
menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents:
40
diff
changeset
|
45 self.menu = Menu(self.loop) |
40 | 46 _menu1 = "Menu 1" |
41
589e5e22f8f1
menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents:
40
diff
changeset
|
47 self.menu.addMenu(_menu1, "Item 1", self.menu_cb) #Adding a menu is quite easy |
589e5e22f8f1
menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents:
40
diff
changeset
|
48 self.menu.addMenu(_menu1, "Item 2", self.menu_cb) #Here the callback is always the same, |
43
e49d8f289a73
menu example: ortho fix in comments :)
Goffi <goffi@goffi.org>
parents:
42
diff
changeset
|
49 self.menu.addMenu(_menu1, "Item 3", self.menu_cb) #but you use different ones in real life :) |
41
589e5e22f8f1
menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents:
40
diff
changeset
|
50 self.menu.addMenu(_menu1, "Exit (C-x)", self.exit_cb, 'ctrl x') #You can also add a shortcut |
40 | 51 _menu2 = "Menu 2" |
41
589e5e22f8f1
menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents:
40
diff
changeset
|
52 self.menu.addMenu(_menu2, "Item 1", self.menu_cb) |
589e5e22f8f1
menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents:
40
diff
changeset
|
53 self.menu.addMenu(_menu2, "Item 2", self.menu_cb) |
589e5e22f8f1
menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents:
40
diff
changeset
|
54 self.menu.addMenu(_menu2, "Item 3", self.menu_cb) |
66 | 55 return self.menu |
40 | 56 |
41
589e5e22f8f1
menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents:
40
diff
changeset
|
57 def keyHandler(self, input): |
42 | 58 """We leave if user press a quit char""" |
40 | 59 if input in ('esc','q','Q'): |
60 raise urwid.ExitMainLoop() | |
41
589e5e22f8f1
menu example: added shortcut example + some comments
Goffi <goffi@goffi.org>
parents:
40
diff
changeset
|
61 else: |
42 | 62 return self.menu.checkShortcuts(input) #needed to manage shortcuts |
40 | 63 |
64 demo = MenuDemo() | |
65 demo.run() |