Repository navigation
Expand file tree
/
Copy pathexample6.py
More file actions
118 lines (91 loc) · 3.67 KB
/
Copy pathexample6.py
File metadata and controls
118 lines (91 loc) · 3.67 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import curses
class MenuDisplay:
def __init__(self, menu):
# set menu parameter as class property
self.menu = menu
# run curses application
curses.wrapper(self.mainloop)
def mainloop(self, stdscr):
# turn off cursor blinking
curses.curs_set(0)
# color scheme for selected row
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
# set screen object as class property
self.stdscr = stdscr
# get screen height and width
self.screen_height, self.screen_width = self.stdscr.getmaxyx()
# specify the current selected row
current_row = 0
# print the menu
self.print_menu(current_row)
while 1:
key = self.stdscr.getch()
if key == curses.KEY_UP and current_row > 0:
current_row -= 1
elif key == curses.KEY_DOWN and current_row < len(self.menu) - 1:
current_row += 1
elif key == curses.KEY_ENTER or key in [10, 13]:
self.print_center("You selected '{}'".format(self.menu[current_row]))
self.stdscr.getch()
# if user selected last row (Exit), confirm before exit the program
if current_row == len(self.menu) - 1:
if self.confirm("Are you sure you want to exit?"):
break
self.print_menu(current_row)
def print_menu(self, selected_row_idx):
self.stdscr.clear()
for idx, row in enumerate(self.menu):
x = self.screen_width // 2 - len(row) // 2
y = self.screen_height // 2 - len(menu) // 2 + idx
if idx == selected_row_idx:
self.color_print(y, x, row, 1)
else:
self.stdscr.addstr(y, x, row)
self.stdscr.refresh()
def color_print(self, y, x, text, pair_num):
self.stdscr.attron(curses.color_pair(pair_num))
self.stdscr.addstr(y, x, text)
self.stdscr.attroff(curses.color_pair(pair_num))
def print_confirm(self, selected="yes"):
# clear yes/no line
curses.setsyx(self.screen_height // 2 + 1, 0)
self.stdscr.clrtoeol()
y = self.screen_height // 2 + 1
options_width = 10
# print yes
option = "yes"
x = self.screen_width // 2 - options_width // 2 + len(option)
if selected == option:
self.color_print(y, x, option, 1)
else:
self.stdscr.addstr(y, x, option)
# print no
option = "no"
x = self.screen_width // 2 + options_width // 2 - len(option)
if selected == option:
self.color_print(y, x, option, 1)
else:
self.stdscr.addstr(y, x, option)
self.stdscr.refresh()
def confirm(self, confirmation_text):
self.print_center(confirmation_text)
current_option = "yes"
self.print_confirm(current_option)
while 1:
key = self.stdscr.getch()
if key == curses.KEY_RIGHT and current_option == "yes":
current_option = "no"
elif key == curses.KEY_LEFT and current_option == "no":
current_option = "yes"
elif key == curses.KEY_ENTER or key in [10, 13]:
return True if current_option == "yes" else False
self.print_confirm(current_option)
def print_center(self, text):
self.stdscr.clear()
x = self.screen_width // 2 - len(text) // 2
y = self.screen_height // 2
self.stdscr.addstr(y, x, text)
self.stdscr.refresh()
if __name__ == "__main__":
menu = ['Home', 'Play', 'Scoreboard', 'Exit']
MenuDisplay(menu)