-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathexample_menu.py
80 lines (63 loc) · 2.29 KB
/
example_menu.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from picotui.widgets import *
from picotui.menu import *
from picotui.context import Context
# Dialog on the screen
d = None
# This routine is called to redraw screen "in menu's background"
def screen_redraw(s, allow_cursor=False):
s.attr_color(C_WHITE, C_BLUE)
s.cls()
s.attr_reset()
d.redraw()
# We have two independent widgets on screen: dialog and main menu,
# so can't call their individual loops, and instead should have
# "main loop" to route events to currently active widget, and
# switch the active one based on special events.
def main_loop():
while 1:
key = m.get_input()
if isinstance(key, list):
# Mouse click
x, y = key
if m.inside(x, y):
m.focus = True
if m.focus:
# If menu is focused, it gets events. If menu is cancelled,
# it loses focus. Otherwise, if menu selection is made, we
# quit with with menu result.
res = m.handle_input(key)
if res == ACTION_CANCEL:
m.focus = False
elif res is not None and res is not True:
return res
else:
# If menu isn't focused, it can be focused by pressing F9.
if key == KEY_F9:
m.focus = True
m.redraw()
continue
# Otherwise, dialog gets input
res = d.handle_input(key)
if res is not None and res is not True:
return res
with Context():
d = Dialog(10, 5, 40, 14)
d.add(12, 1, WLabel("Press F9 for menu"))
d.add(1, 2, WLabel("Label:"))
d.add(1, 3, WListBox(16, 4, ["choice%d" % i for i in range(10)]))
d.add(1, 8, WDropDown(10, ["Red", "Green", "Yellow"]))
b = WButton(8, "OK")
d.add(10, 11, b)
b.finish_dialog = ACTION_OK
b = WButton(8, "Cancel")
d.add(23, 11, b)
b.finish_dialog = ACTION_CANCEL
screen_redraw(Screen)
Screen.set_screen_redraw(screen_redraw)
menu_file = WMenuBox([("Open...", "Open"), ("Save", "S"), ("Save as...", "Sa"), ("Exit", "ex")])
menu_edit = WMenuBox([("Copy", "copy"), ("Paste", "paste")])
m = WMenuBar([("File", menu_file), ("Edit", menu_edit), ("About", "About")])
m.permanent = True
m.redraw()
res = main_loop()
print("Result:", res)