self.select_auto()
self.view.update_controls()
+ def rename_student(self, curr_name, new_name):
+ if new_name != curr_name:
+ if new_name in self.data:
+ raise ValueError("Name already in use")
+ else:
+ data = self.data[curr_name]
+ self.data[new_name] = data
+ del self.data[curr_name]
+ self.view.set_student_list(self.view_names())
+ curr = new_name if self.state['student'] == curr_name else self.state['student']
+ self.select_student(curr)
+
def select_auto(self):
names = self.view_names()
self.select_student(names[0] if names else None)
self.view.set_student(name)
-class AsyncHttpServer:
- def __init__(self, port, data):
- class Handler(http.server.BaseHTTPRequestHandler):
- def __init__(self): pass
- def __call__(self, *args): super().__init__(*args)
-
- def do_GET(self):
- self.send_response(200)
- self.send_header('Content-Type', 'application/json; charset=UTF-8')
- self.send_header('Access-Control-Allow-Origin', '*')
- self.end_headers()
- self.wfile.write(data)
-
- self.server = http.server.HTTPServer(('localhost', port), Handler())
-
- def start(self):
- Thread(target = lambda: self.server.serve_forever()).start()
-
- def stop(self):
- self.server.shutdown()
- self.server.server_close()
-
class MainView(View):
@contextmanager
def no_propagate(self, key):
messagebox.showerror("Error", "Student already exists")
add_student_btn = ttk.Button(self.student_frame, text = "Add", command = on_student_add)
+ def on_student_rename():
+ curr = self.controller.current_student()
+ name = simpledialog.askstring("Rename student", "New name for {}".format(curr))
+ if name:
+ try:
+ self.controller.rename_student(curr, name)
+ except ValueError as e:
+ messagebox.showerror("Error", str(e))
+ self.rename_student_btn = ttk.Button(self.student_frame, text = "Rename", command = on_student_rename)
+
def on_student_del():
curr_name = self.controller.current_student()
result = messagebox.askokcancel("Confirm", "Delete student {}?".format(curr_name))
self.student_sel.pack(**TK_PAD, side = tk.LEFT)
add_student_btn.pack(**TK_PAD, side = tk.LEFT)
+ self.rename_student_btn.pack(**TK_PAD, side = tk.LEFT)
self.del_student_btn.pack(**TK_PAD, side = tk.LEFT)
self.student_frame.pack(**TK_PAD, fill = tk.X)
self.edit_data_btn.pack(**TK_PAD, fill = tk.X)
state = 'enable' if self.controller.data else 'disable'
students_only = [
self.student_sel,
+ self.rename_student_btn,
self.del_student_btn,
self.edit_data_btn,
self.gen_plot_btn,