Skip to content

Commit

Permalink
Merge pull request #16 from askmarkio/save-prompt
Browse files Browse the repository at this point in the history
Fixed #9 - App will now track changes and prompt to save when closing
  • Loading branch information
askmarkio authored Jun 10, 2024
2 parents 67b5477 + 4fa47c5 commit b32f902
Showing 1 changed file with 56 additions and 14 deletions.
70 changes: 56 additions & 14 deletions libderegtxt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,31 @@
from PIL import Image, ImageTk
import os

# Variables to track file path and changes
file_path = None
text_changed = False

# Creating all the functions of all the buttons in the Deregtext
def open_file():
"""
Function to open files.
The default extension is ".dgt"
"""

global file_path, text_changed
file = fd.askopenfilename(
defaultextension=".dgt",
filetypes=[("All Files", "*.*"), ("Text File", "*.dgt")],
)

if file != "":
root.title(f"{os.path.basename(file)}")
text_area.delete(1.0, END)
text_area.delete(1.0, END) # This deletes all text in the text area
with open(file, "r") as file_:
text_area.insert(1.0, file_.read())
file_.close()
file_path = file
text_changed = False
else:
file = None

Expand All @@ -31,26 +38,54 @@ def open_new_file():
Opens a new file and deletes all content in the text area.
"""

global file_path, text_changed
root.title("Untitled - Deregtext")
text_area.delete(1.0, END)
file_path = None
text_changed = False

def save_file():
"""
Function to save file.
Provides defaults for the user to overwrite.
"""

global text_area
file_path = fd.asksaveasfilename(initialfile='Untitled.dgt', defaultextension='.txt', filetypes=[("Text File", "*.txt"), ("Word Document", '*.docx'), ("PDF", "*.pdf")])
if file_path:
with open(file_path, "w") as file:
file.write(text_area.get(1.0, END))
root.title(f"{os.path.basename(file_path)} - Deregtext")
global file_path, text_changed
if file_path is None:
file_path = fd.asksaveasfilename(
initialfile="Untitled.dgt",
defaultextension=".dgt",
filetypes=[
("Text File", "*.txt"),
("Word Document", "*.docx"),
("PDF", "*.pdf"),
],
)
if not file_path:
return

def exit_application():
root.destroy()
with open(file_path, "w") as file:
file.write(text_area.get(1.0, END))
root.title(f"{os.path.basename(file_path)} - Deregtext")
text_changed = False


def exit_application():
if text_changed:
response = mb.askyesnocancel("Save changes", "Do you want to save your changes?")
if response: # Yes
save_file()
root.destroy()
elif response is None: # Cancel
return
else: # No
root.destroy()
else:
root.destroy()

def on_text_change(event=None):
global text_changed
text_changed = True
def copy_text():
text_area.event_generate("<<Copy>>")

Expand All @@ -69,16 +104,16 @@ def select_all():
'sel' defines the selection while '1.0' denotes the
start point and the end point is 'end'
"""
text_area.tag_add('sel', '1.0', 'end')
return 'break'
text_area.tag_add("sel", "1.0", "end")
return "break"


def delete_last_char():
text_area.event_generate("<<KP_Delete>>")


def about_deregtext():

aboutdereg = """
About Deregtext
Expand All @@ -87,7 +122,7 @@ def about_deregtext():
Python. I have learned a bit as I've squashed bugs and
implemented some features. Stay tuned.
"""

custom_dialog1 = Toplevel(root)
custom_dialog1.title("About Deregtext")
custom_dialog1.geometry("610x400")
Expand All @@ -98,6 +133,7 @@ def about_deregtext():
button = Button(custom_dialog1, text="OK", command=custom_dialog1.destroy)
button.pack(pady=10)


def about_commands():
commands = """
Under the File Menu:
Expand Down Expand Up @@ -208,20 +244,26 @@ def hide_context_menu(event):
text_area.config(yscrollcommand=scroller.set)

# Bind the select_all function to a keyboard shortcut (Ctrl+A)
root.bind('<Control-a>', select_all)
root.bind("<Control-a>", select_all)

# Bind the right-click event to show the context menu
text_area.bind("<Button-3>", show_context_menu)

# Bind the left-click event to hide the context menu
text_area.bind("<Button-1>", hide_context_menu)

# Bind text change event to track changes
text_area.bind('<<Modified>>', on_text_change)

# Give the text area focus when the application starts
text_area.focus_set()

# Overrie the window close button to prompt to save changes
root.protocol("WM_DELETE_WINDOW", exit_application)
# Finalizing the window
# root.update()
root.mainloop()


if __name__ == "__main__":
main()

0 comments on commit b32f902

Please sign in to comment.