Skip to content

Commit

Permalink
File saving
Browse files Browse the repository at this point in the history
Added functions to save a file.
  • Loading branch information
Claydough6 committed May 4, 2021
1 parent f614428 commit 73890ca
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 2 deletions.
10 changes: 10 additions & 0 deletions edit_listbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,14 @@ def remove_premise(self):
index = select[0]
self.delete(index)
self.colorize()

def get_save_string(self):
string = "<premise>\n"
for i in range(self.size()):
text = self.get(i)
string += text + "\n"
string += "<\premise>\n"
return string



21 changes: 19 additions & 2 deletions gui_main.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down
55 changes: 55 additions & 0 deletions resolution_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 += "<top-clause>\n"

# get the id
string += "<id>{}</id>\n".format(frame.id)

# get the position
bbox = self.bbox(fid)
x = (bbox[0] + bbox[2]) / 2
y = (bbox[1] + bbox[3]) / 2
string += "<pos>{},{}</pos>\n".format(x, y)

# get the sentance
string+= "<sen>{}</sen>\n".format(frame.text.get())

# get the premise index
string += "<prem>{}</prem>\n".format(frame.premise_index)

# end it
string += "</top-clause>\n"

elif frame.state == "clause":
# get the type
string += "<clause>\n"

# get the id
string += "<id>{}</id>\n".format(frame.id)

# get the position
bbox = self.bbox(fid)
x = (bbox[0] + bbox[2]) / 2
y = (bbox[1] + bbox[3]) / 2
string += "<pos>{},{}</pos>\n".format(x, y)

# get the sentance
string+= "<sen>{}</sen>\n".format(frame.text.get())

# get the premise index
ps = ""
for item in frame.parents:
ps += str(item) + ','
string += "<parents>{}</parents>\n".format(ps[:-1])

# end it
string += "</clause>\n"
string += "\n"

return string



8 changes: 8 additions & 0 deletions statement_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

22 changes: 22 additions & 0 deletions testing/file_template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<premise>
P | Q
A & B
<\premise>

<conclusion>
~P
</conclusion>

<top-clause>
<id>1<\id>
<pos>120,35<\pos>
<sen>{ P, Q }<\sen>
<prem>1<\prem>
<\top-clause>

<clause>
<id>1<\id>
<pos>120,35<\pos>
<sen>{ P, Q }<\sen>
<parents>3,4<\parents>
<\clause>

0 comments on commit 73890ca

Please sign in to comment.