From 73890caf757f4dbf7cc4dff0dde44d69bdc882b0 Mon Sep 17 00:00:00 2001 From: Claydough6 Date: Tue, 4 May 2021 18:13:45 -0400 Subject: [PATCH] File saving Added functions to save a file. --- edit_listbox.py | 10 +++++++ gui_main.py | 21 +++++++++++++-- resolution_canvas.py | 55 +++++++++++++++++++++++++++++++++++++++ statement_frame.py | 8 ++++++ testing/file_template.txt | 22 ++++++++++++++++ 5 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 testing/file_template.txt diff --git a/edit_listbox.py b/edit_listbox.py index eeeb3f3..f152161 100644 --- a/edit_listbox.py +++ b/edit_listbox.py @@ -91,4 +91,14 @@ def remove_premise(self): index = select[0] self.delete(index) self.colorize() + + def get_save_string(self): + string = "\n" + for i in range(self.size()): + text = self.get(i) + string += text + "\n" + string += "<\premise>\n" + return string + + diff --git a/gui_main.py b/gui_main.py index 68ddf0a..cc52db8 100644 --- a/gui_main.py +++ b/gui_main.py @@ -1,6 +1,7 @@ from tkinter import * from tkinter import ttk from ttkthemes import ThemedTk +from tkinter import filedialog from resolution_canvas import ResolutionCanvas from statement_frame import StatementFrame @@ -10,6 +11,7 @@ class ResolutionGUI(): def __init__(self): # useful variables self.__version__ = 0.3 + self.filename = None # used to help transfer between subapps self.selected_clause_id = None @@ -86,11 +88,26 @@ def create_menu(self): self.menubar.add_command(label="Help", command=self.get_help) self.filemenu.add_command(label='Open') - self.filemenu.add_command(label='Save') - self.filemenu.add_command(label='Save As') + self.filemenu.add_command(label='Save', command=self.save) + self.filemenu.add_command(label='Save As', command=self.saveas) self.root.configure(menu=self.menubar) + def save(self, *args): + #InfoWindow(self.root, "Save Window!", self.canvas.get_save_string()) + if not self.filename: + self.saveas() + else: + savestr = "" + savestr += self.leftframe.get_save_string() + savestr += self.canvas.get_save_string() + with open(self.filename, 'w') as f: + f.write(savestr) + + def saveas(self, *args): + self.filename = filedialog.asksaveasfilename() + self.save() + # grid everything into the app def grid(self): self.resolution.grid(row=0, column=1, rowspan=3, padx=5, pady=5, sticky=(N,E,S,W)) diff --git a/resolution_canvas.py b/resolution_canvas.py index 06e004f..0dc9521 100644 --- a/resolution_canvas.py +++ b/resolution_canvas.py @@ -199,5 +199,60 @@ def deselect(self): self.frames[frame].configure(relief="flat") self.dtag("selected") + + def get_save_string(self): + string = "" + for fid in self.find_withtag("statement"): + frame = self.frames[fid] + if frame.state == "topclause": + # get the type + string += "\n" + + # get the id + string += "{}\n".format(frame.id) + + # get the position + bbox = self.bbox(fid) + x = (bbox[0] + bbox[2]) / 2 + y = (bbox[1] + bbox[3]) / 2 + string += "{},{}\n".format(x, y) + + # get the sentance + string+= "{}\n".format(frame.text.get()) + + # get the premise index + string += "{}\n".format(frame.premise_index) + + # end it + string += "\n" + + elif frame.state == "clause": + # get the type + string += "\n" + + # get the id + string += "{}\n".format(frame.id) + + # get the position + bbox = self.bbox(fid) + x = (bbox[0] + bbox[2]) / 2 + y = (bbox[1] + bbox[3]) / 2 + string += "{},{}\n".format(x, y) + + # get the sentance + string+= "{}\n".format(frame.text.get()) + + # get the premise index + ps = "" + for item in frame.parents: + ps += str(item) + ',' + string += "{}\n".format(ps[:-1]) + + # end it + string += "\n" + string += "\n" + + return string + diff --git a/statement_frame.py b/statement_frame.py index bc9ed7c..5c83929 100644 --- a/statement_frame.py +++ b/statement_frame.py @@ -56,4 +56,12 @@ def grid_widgets(self): # set the column and row configurations self.statements.grid_columnconfigure(0, weight=1) self.statements.grid_rowconfigure(0, weight=1) + + def get_save_string(self): + string = "" + string += self.plist.get_save_string() + string += "\n" + string += self.clist.get_save_string() + string += "\n" + return string diff --git a/testing/file_template.txt b/testing/file_template.txt new file mode 100644 index 0000000..6d7b0ee --- /dev/null +++ b/testing/file_template.txt @@ -0,0 +1,22 @@ + +P | Q +A & B +<\premise> + + +~P + + + +1<\id> +120,35<\pos> +{ P, Q }<\sen> +1<\prem> +<\top-clause> + + +1<\id> +120,35<\pos> +{ P, Q }<\sen> +3,4<\parents> +<\clause> \ No newline at end of file