easy_tui.examples
1def window_example(): 2 """Example of the Window class""" 3 4 from window import Window 5 from os import system 6 7 WINDOW_HEIGHT = 27 8 WINDOW_WIDTH = 140 9 10 class Example(Window): 11 def __init__(self): 12 self.window_height = WINDOW_HEIGHT 13 self.new_buffer() # Creates a new datamember: self.buffer. 14 15 self.buffer[0] = "This is the first line" 16 self.buffer[1] = "This is the second line" 17 self.buffer[2] = "=================================" 18 self.buffer[3] = "..." 19 self.buffer[-1] = "This is the last line. Press [ENTER] to continue" 20 21 def update_buffer_once(self): 22 self.buffer[7] = "This is an updated line" 23 self.buffer[8] = "Notice that we only have to mess with the lines that are changing" 24 25 def update_buffer_twice(self): 26 self.wipe() 27 self.buffer[8] = "Everything was wiped!!!" 28 29 system(f"mode {WINDOW_WIDTH}, {WINDOW_HEIGHT+1}") # Sets the terminal window size. 30 # Works better in a self-contained terminal window (not code editor) 31 w = Example() 32 33 print(w) 34 input() 35 36 w.update_buffer_once() 37 print(w) 38 input() 39 40 w.update_buffer_twice() 41 print(w) 42 input() 43 44def window_with_menu_example(): 45 """Example of the Menu class implemented on a custom Window instance""" 46 47 from window import Window 48 from menu import Menu 49 from os import system 50 51 WINDOW_HEIGHT = 27 52 WINDOW_WIDTH = 140 53 54 class Example(Window): 55 def __init__(self): 56 self.window_height = WINDOW_HEIGHT 57 self.new_buffer() # Creates a new datamember: self.buffer. 58 59 self.menu = self.init_menu() 60 61 self.buffer[0] = "Example Window. See options below." 62 self.buffer[1] = "==================================" 63 self.buffer[2] = "" 64 65 self.buffer_menu() 66 67 self.buffer[-2] = 'Enter selection. Type "up", "down", or "exit"' 68 69 def init_menu(self): 70 menu_options = [ 71 "This is option idx = 0", 72 "This is option idx = 1", 73 "This is option idx = 2" 74 ] 75 return Menu(menu_options) 76 77 def buffer_menu(self): 78 for i in range(len(self.menu.options)): 79 if self.menu.selection == i: 80 self.buffer[i+3] = f"[X] {self.menu.options[i]}" 81 else: 82 self.buffer[i+3] = f"[ ] {self.menu.options[i]}" 83 84 def select_up(self): 85 self.menu.select_up() 86 self.buffer_menu() 87 88 def select_down(self): 89 self.menu.select_down() 90 self.buffer_menu() 91 92 w = Example() 93 print(w) 94 95 while(True): 96 i = input() 97 if i.lower() == "exit": 98 break 99 elif i.lower() == "up": 100 w.select_up() 101 print(w) 102 continue 103 elif i.lower() == "down": 104 w.select_down() 105 print(w) 106 continue 107 print(w) 108 109def window_with_table_menu_example(): 110 """Example of the TableMenu class implemented on a custom Window instance""" 111 112 from window import Window 113 from menu import TableMenu 114 from os import system 115 116 WINDOW_HEIGHT = 27 117 WINDOW_WIDTH = 140 118 119 class Example(Window): 120 def __init__(self): 121 self.window_height = WINDOW_HEIGHT 122 self.new_buffer() # Creates a new datamember: self.buffer. 123 124 self.menu = self.init_menu() 125 126 self.buffer[0] = "Example Window. See options below." 127 self.buffer[1] = "==================================" 128 self.buffer[2] = "" 129 130 self.buffer_menu() 131 132 self.buffer[-2] = 'Enter selection. Type "up", "down", "add", "remove", or "exit"' 133 134 def init_menu(self): 135 menu_options = [ 136 "This is option idx = 0", 137 "This is option idx = 1", 138 "This is option idx = 2" 139 ] 140 return TableMenu(menu_options) 141 142 def buffer_menu(self): 143 for i in range(len(self.menu.options)): 144 if i == self.menu.selection: 145 self.buffer[i+3] = 'X ' 146 else: 147 self.buffer[i+3] = ' ' 148 149 if i in self.menu.selections: 150 self.buffer[i+3] += f"[*] {self.menu.options[i]}" 151 else: 152 self.buffer[i+3] += f"[ ] {self.menu.options[i]}" 153 154 def select_up(self): 155 self.menu.select_up() 156 self.buffer_menu() 157 158 def select_down(self): 159 self.menu.select_down() 160 self.buffer_menu() 161 162 def add_selection(self): 163 self.menu.add_selection() 164 self.buffer_menu() 165 166 def remove_selection(self): 167 self.menu.rm_selection() 168 self.buffer_menu() 169 170 w = Example() 171 print(w) 172 173 while(True): 174 i = input() 175 if i.lower() == "exit": 176 break 177 elif i.lower() == "up": 178 w.select_up() 179 print(w) 180 continue 181 elif i.lower() == "down": 182 w.select_down() 183 print(w) 184 continue 185 elif i.lower() == "add": 186 w.add_selection() 187 print(w) 188 continue 189 elif i.lower() == "remove": 190 w.remove_selection() 191 print(w) 192 continue 193 print(w)
def
window_example():
2def window_example(): 3 """Example of the Window class""" 4 5 from window import Window 6 from os import system 7 8 WINDOW_HEIGHT = 27 9 WINDOW_WIDTH = 140 10 11 class Example(Window): 12 def __init__(self): 13 self.window_height = WINDOW_HEIGHT 14 self.new_buffer() # Creates a new datamember: self.buffer. 15 16 self.buffer[0] = "This is the first line" 17 self.buffer[1] = "This is the second line" 18 self.buffer[2] = "=================================" 19 self.buffer[3] = "..." 20 self.buffer[-1] = "This is the last line. Press [ENTER] to continue" 21 22 def update_buffer_once(self): 23 self.buffer[7] = "This is an updated line" 24 self.buffer[8] = "Notice that we only have to mess with the lines that are changing" 25 26 def update_buffer_twice(self): 27 self.wipe() 28 self.buffer[8] = "Everything was wiped!!!" 29 30 system(f"mode {WINDOW_WIDTH}, {WINDOW_HEIGHT+1}") # Sets the terminal window size. 31 # Works better in a self-contained terminal window (not code editor) 32 w = Example() 33 34 print(w) 35 input() 36 37 w.update_buffer_once() 38 print(w) 39 input() 40 41 w.update_buffer_twice() 42 print(w) 43 input()
Example of the Window class