-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibderegtxt.py
269 lines (204 loc) · 7.68 KB
/
libderegtxt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from tkinter import *
import tkinter.filedialog as fd
import tkinter.messagebox as mb
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) # 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
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 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
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>>")
def cut_text():
text_area.event_generate("<<Cut>>")
def paste_text():
text_area.event_generate("<<Paste>>")
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("<<KP_Delete>>")
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("<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()