From c8fd1e8925d16a94cc2c273ea084884b1faeb6e6 Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 9 Jun 2024 23:35:24 -0500 Subject: [PATCH] Cleanup #11 - some code cleanup and organizing - removed test text files - updated .gitignore to block python cache - renamed project directory - renamed main py file - created main executible - retructured code for remote execution to avoid race conditions --- .gitignore | 2 + Second-file.dgt | 1 - deregtxt | 15 ++ first-note.dgt | 1 - libderegtxt.py | 227 +++++++++++++++++++++++++++ Notepad.png => media/img/Notepad.png | Bin texteditor.py | 223 -------------------------- 7 files changed, 244 insertions(+), 225 deletions(-) create mode 100644 .gitignore delete mode 100644 Second-file.dgt create mode 100755 deregtxt delete mode 100644 first-note.dgt create mode 100644 libderegtxt.py rename Notepad.png => media/img/Notepad.png (100%) delete mode 100644 texteditor.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0c4323f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.mypy_cache +__pycache__ diff --git a/Second-file.dgt b/Second-file.dgt deleted file mode 100644 index 022c657..0000000 --- a/Second-file.dgt +++ /dev/null @@ -1 +0,0 @@ -I got save working. diff --git a/deregtxt b/deregtxt new file mode 100755 index 0000000..98b982d --- /dev/null +++ b/deregtxt @@ -0,0 +1,15 @@ +#!/usr/bin/env python3 + +import libderegtxt + + +# Package meta-data. +NAME = 'Deregtext' +DESCRIPTION = 'Text editor software' +URL = 'https://github.com/askmarkio/deregtext' +EMAIL = 'mark@askmark.io' +AUTHOR = 'Mark' +REQUIRES_PYTHON = '>=3.6.0' +VERSION = '0.1.0' + +libderegtxt.main() diff --git a/first-note.dgt b/first-note.dgt deleted file mode 100644 index 12808f0..0000000 --- a/first-note.dgt +++ /dev/null @@ -1 +0,0 @@ -This is my first file from writing my very own text edit app. diff --git a/libderegtxt.py b/libderegtxt.py new file mode 100644 index 0000000..fe2ae8d --- /dev/null +++ b/libderegtxt.py @@ -0,0 +1,227 @@ +from tkinter import * +import tkinter.filedialog as fd +import tkinter.messagebox as mb +from PIL import Image, ImageTk +import os + +# Creating all the functions of all the buttons in the Deregtext +def open_file(): + """ + Function to open files. + The default extension is ".dgt" + """ + + 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) + with open(file, "r") as file_: + text_area.insert(1.0, file_.read()) + file_.close() + else: + file = None + + +def open_new_file(): + """ + Opens a new file and deletes all content in the text area. + """ + + root.title("Untitled - Deregtext") + text_area.delete(1.0, END) + +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") + +def exit_application(): + root.destroy() + + +def copy_text(): + text_area.event_generate("<>") + + +def cut_text(): + text_area.event_generate("<>") + + +def paste_text(): + text_area.event_generate("<>") + + +def select_all(): + """ + Function to select all text within the text area. + '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' + + +def delete_last_char(): + text_area.event_generate("<>") + + +def about_deregtext(): + + aboutdereg = """ + About Deregtext + + This is the first application from the DeregSoftware suite + of apps. In reality, this is a coding project to learn + 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") + + label = Label(custom_dialog1, text=aboutdereg, justify=LEFT) + label.pack(padx=10, pady=10) + + button = Button(custom_dialog1, text="OK", command=custom_dialog1.destroy) + button.pack(pady=10) + +def about_commands(): + commands = """ + Under the File Menu: + - 'New' clears the entire Text text_area + - 'Open' clears text and opens another file + - 'Save As' saves your file in the same / another extension + + Under the Edit Menu: + - 'Copy' copies the selected text to your clipboard + - 'Cut' cuts the selected text and removes it from the text area + - 'Paste' pastes the copied/cut text + - 'Select All' selects the entire text + - 'Delete' deletes the last character + """ + + custom_dialog = Toplevel(root) + custom_dialog.title("All commands") + custom_dialog.geometry("610x500") + + label = Label(custom_dialog, text=commands, justify=LEFT) + label.pack(padx=10, pady=10) + + button = Button(custom_dialog, text="OK", command=custom_dialog.destroy) + button.pack(pady=10) + + +def main(): + """ + Main is defined to allow remote execution of program. + This helps ensure that all necessary items are loaded + before the program can be executed, else, the executer + will error out. + """ + + global root, text_area + + # Initializing the window to create python text editor + root = Tk() + root.title("Untitled - Deregtext") + root.geometry("1000x700") + root.resizable(TRUE, TRUE) + + root.columnconfigure(0, weight=1) + root.rowconfigure(0, weight=1) + + icon = ImageTk.PhotoImage(Image.open("media/img/Notepad.png")) + root.iconphoto(False, icon) + + # Create the context menu + context_menu = Menu(root, tearoff=0) + context_menu.add_command(label="Copy", command=copy_text) + context_menu.add_command(label="Cut", command=cut_text) + context_menu.add_command(label="Paste", command=paste_text) + context_menu.add_separator() + context_menu.add_command(label="Select All", command=select_all) + context_menu.add_command(label="Delete", command=delete_last_char) + + def show_context_menu(event): + context_menu.post(event.x_root, event.y_root) + + def hide_context_menu(event): + context_menu.unpost() + + # Create the menu bar + menu_bar = Menu(root) + + # Adding the File Menu and its components to create Python Text Editor + file_menu = Menu(menu_bar, tearoff=False, activebackground="DodgerBlue") + + file_menu.add_command(label="New", command=open_new_file) + file_menu.add_command(label="Open File", command=open_file) + file_menu.add_command(label="Save As", command=save_file) + file_menu.add_separator() + file_menu.add_command(label="Close File", command=exit_application) + + menu_bar.add_cascade(label="File", menu=file_menu) + + # Adding the Edit Menu and its components + edit_menu = Menu(menu_bar, tearoff=False, activebackground="DodgerBlue") + + edit_menu.add_command(label="Copy", command=copy_text) + edit_menu.add_command(label="Cut", command=cut_text) + edit_menu.add_command(label="Paste", command=paste_text) + edit_menu.add_separator() + edit_menu.add_command(label="Select All", command=select_all) + edit_menu.add_command(label="Delete", command=delete_last_char) + + menu_bar.add_cascade(label="Edit", menu=edit_menu) + + # Adding the Help Menu and its components + help_menu = Menu(menu_bar, tearoff=False, activebackground="DodgerBlue") + + help_menu.add_command(label="About Deregtext", command=about_deregtext) + help_menu.add_command(label="About Commands", command=about_commands) + + menu_bar.add_cascade(label="Help", menu=help_menu) + + root.config(menu=menu_bar) + + # Setting the basic components of the window + text_area = Text(root, font=("Times New Roman", 12)) + text_area.grid(sticky=NSEW) + + scroller = Scrollbar(text_area, orient=VERTICAL) + scroller.pack(side=RIGHT, fill=Y) + + scroller.config(command=text_area.yview) + text_area.config(yscrollcommand=scroller.set) + + # Bind the select_all function to a keyboard shortcut (Ctrl+A) + root.bind('', select_all) + + # Bind the right-click event to show the context menu + text_area.bind("", show_context_menu) + + # Bind the left-click event to hide the context menu + text_area.bind("", hide_context_menu) + + # Give the text area focus when the application starts + text_area.focus_set() + + # Finalizing the window + # root.update() + root.mainloop() + +if __name__ == "__main__": + main() diff --git a/Notepad.png b/media/img/Notepad.png similarity index 100% rename from Notepad.png rename to media/img/Notepad.png diff --git a/texteditor.py b/texteditor.py deleted file mode 100644 index a3197e8..0000000 --- a/texteditor.py +++ /dev/null @@ -1,223 +0,0 @@ -from tkinter import * -import tkinter.filedialog as fd -import tkinter.messagebox as mb -from PIL import Image, ImageTk -import os - -# Initializing the window to create python text editor -root = Tk() -root.title("Untitled - Deregtext") -root.geometry("1000x700") -root.resizable(TRUE, TRUE) - -root.columnconfigure(0, weight=1) -root.rowconfigure(0, weight=1) - -icon = ImageTk.PhotoImage(Image.open("Notepad.png")) -root.iconphoto(False, icon) - - -# Creating all the functions of all the buttons in the Deregtext -def open_file(): - 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) - with open(file, "r") as file_: - text_area.insert(1.0, file_.read()) - file_.close() - else: - file = None - - -def open_new_file(): - root.title("Untitled - Deregtext") - text_area.delete(1.0, END) - - -# def save_file(): -# global text_area -# file = text_area.get(1.0, END) -# if file == "": -# file = None -# else: -# file = open(file, "w") -# file.write(text_area.get(1.0, END)) -# file.close() -# -# if file is None: -# file = fd.asksaveasfilename( -# initialfile="Untitled.dgt", -# defaultextension=".dgt", -# filetypes=[ -# ("Text File", "*.dgt*"), -# ("Word Document", "*.docx*"), -# ("PDF", "*.pdf*"), -# ], -# ) -# else: -# file = open(file, "w") -# file.write(text_area.get(1.0, END)) -# file.close() -# root.title(f"{os.path.basename(file)} = Deregtext") - - -def save_file(): - 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") - -def exit_application(): - root.destroy() - - -def copy_text(): - text_area.event_generate("<>") - - -def cut_text(): - text_area.event_generate("<>") - - -def paste_text(): - text_area.event_generate("<>") - - -def select_all(): - text_area.tag_add('sel', '1.0', 'end') - return 'break' - - -def delete_last_char(): - text_area.event_generate("<>") - - -def about_deregtext(): - - aboutdereg = """ - About Deregtext - - This is the first application from the DeregSoftware suite - of apps. In reality, this is a coding project to learn - 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") - - label = Label(custom_dialog1, text=aboutdereg, justify=LEFT) - label.pack(padx=10, pady=10) - - button = Button(custom_dialog1, text="OK", command=custom_dialog1.destroy) - button.pack(pady=10) - -def about_commands(): - commands = """ - Under the File Menu: - - 'New' clears the entire Text text_area - - 'Open' clears text and opens another file - - 'Save As' saves your file in the same / another extension - - Under the Edit Menu: - - 'Copy' copies the selected text to your clipboard - - 'Cut' cuts the selected text and removes it from the text area - - 'Paste' pastes the copied/cut text - - 'Select All' selects the entire text - - 'Delete' deletes the last character - """ - - custom_dialog = Toplevel(root) - custom_dialog.title("All commands") - custom_dialog.geometry("610x500") - - label = Label(custom_dialog, text=commands, justify=LEFT) - label.pack(padx=10, pady=10) - - button = Button(custom_dialog, text="OK", command=custom_dialog.destroy) - button.pack(pady=10) - -# Create the context menu -context_menu = Menu(root, tearoff=0) -context_menu.add_command(label="Copy", command=copy_text) -context_menu.add_command(label="Cut", command=cut_text) -context_menu.add_command(label="Paste", command=paste_text) -context_menu.add_separator() -context_menu.add_command(label="Select All", command=select_all) -context_menu.add_command(label="Delete", command=delete_last_char) - -def show_context_menu(event): - context_menu.post(event.x_root, event.y_root) - -def hide_context_menu(event): - context_menu.unpost() - -# Create the menu bar -menu_bar = Menu(root) - -# Adding the File Menu and its components to create Python Text Editor -file_menu = Menu(menu_bar, tearoff=False, activebackground="DodgerBlue") - -file_menu.add_command(label="New", command=open_new_file) -file_menu.add_command(label="Open File", command=open_file) -file_menu.add_command(label="Save As", command=save_file) -file_menu.add_separator() -file_menu.add_command(label="Close File", command=exit_application) - -menu_bar.add_cascade(label="File", menu=file_menu) - -# Adding the Edit Menu and its components -edit_menu = Menu(menu_bar, tearoff=False, activebackground="DodgerBlue") - -edit_menu.add_command(label="Copy", command=copy_text) -edit_menu.add_command(label="Cut", command=cut_text) -edit_menu.add_command(label="Paste", command=paste_text) -edit_menu.add_separator() -edit_menu.add_command(label="Select All", command=select_all) -edit_menu.add_command(label="Delete", command=delete_last_char) - -menu_bar.add_cascade(label="Edit", menu=edit_menu) - -# Adding the Help Menu and its components -help_menu = Menu(menu_bar, tearoff=False, activebackground="DodgerBlue") - -help_menu.add_command(label="About Deregtext", command=about_deregtext) -help_menu.add_command(label="About Commands", command=about_commands) - -menu_bar.add_cascade(label="Help", menu=help_menu) - -root.config(menu=menu_bar) - -# Setting the basic components of the window -text_area = Text(root, font=("Times New Roman", 12)) -text_area.grid(sticky=NSEW) - -scroller = Scrollbar(text_area, orient=VERTICAL) -scroller.pack(side=RIGHT, fill=Y) - -scroller.config(command=text_area.yview) -text_area.config(yscrollcommand=scroller.set) - -# Bind the select_all function to a keyboard shortcut (Ctrl+A) -root.bind('', select_all) - -# Bind the right-click event to show the context menu -text_area.bind("", show_context_menu) - -# Bind the left-click event to hide the context menu -text_area.bind("", hide_context_menu) - -# Give the text area focus when the application starts -text_area.focus_set() - -# Finalizing the window -# root.update() -root.mainloop()