-
Notifications
You must be signed in to change notification settings - Fork 1
/
menu.py
57 lines (50 loc) · 1.63 KB
/
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
#Menu class made by me, using the singleton window class
import window, easykeys as keys, tolk
class MenuError(Exception):
pass
class menu:
def __init__(self, items, win, first_letter=True):
self.items=items
self.window=win
if win.shown==False:
raise MenuError("You must show a window if you want to get any keyboard input. The window you passed expects to be shown.")
self.keys='abcdefghijklmnopqrstuvwxyz'
self.first_letter=first_letter
def __getitem__(self, ind):
ind+=1
if ind>=len(self.items):
ind=0
elif ind<0:
ind=len(self.items)-1
return self.items[ind]
def run(self, start=0, prompt="Please choose an option", callback=None):
tolk.say(prompt)
while True:
self.window.wait(20)
position=start
if self.window.key_pressed(keys.k_down):
position+=1
if self.window.key_pressed(keys.k_up):
position-=1
if position<0:
position=len(self.items)-1
elif position>=len(self.items):
position=0
if self.window.key_pressed(keys.k_escape):
return -1
if self.window.key_pressed(keys.k_enter):
return self.items[position]
if callable(callback)==True and callback((position, self[position]))==False: return self[position]
if self.first_letter==True:
for i in self.keys:
if self.window.key_pressed(keys.get('k_'+i)):
if self.window.pressing(keys.k_shift): i=i.upper()
list=self.items[position+1:]
list+=self.items[:position+1]
for j in list:
if j[0]==i:
position=self.items.index(j)
break
if position!=start:
tolk.say(self.items[position])
start=position