-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
270 lines (219 loc) · 9.13 KB
/
main.py
File metadata and controls
270 lines (219 loc) · 9.13 KB
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
270
from tkinter import *
from tkinter import filedialog
from tkinter import font
from tkinter import colorchooser
import win32api
root = Tk()
root.title("Text Editor")
root.geometry("1200x710")
global open_status_name
open_status_name = False
global selected
selected = False
def New_file():
text.delete("1.0", END)
root.title("Opened New File - Text Editor")
status_bar.config(text="New File ")
global open_status_name
open_status_name = False
def open_file():
text.delete("1.0", END)
text_file = filedialog.askopenfilename(title="Open File", filetypes=[("Text Files", "*.txt")])
if text_file:
global open_status_name
open_status_name = text_file
name = text_file
status_bar.config(text=f"{name} ")
root.title(f"{name} - Text Editor")
text_file = open(text_file, 'r')
text_file_content = text_file.read()
text.insert(END, text_file_content)
text_file.close
def save_as_file():
text_file = filedialog.asksaveasfilename(defaultextension=".*", title="Save File", filetypes=[("Text Files", "*.txt")])
if text_file:
name = text_file
status_bar.config(text=f"{name} Saved ")
root.title(f"{name} - Text Editor")
text_file = open(text_file, "w")
text_file.write(text.get(1.0, END))
text_file.close()
def save_file():
global open_status_name
if open_status_name:
text_file = open(open_status_name, 'w')
text_file.write(text.get(1.0, END))
text_file.close()
status_bar.config(text=f'Saved {open_status_name}')
else:
save_as_file()
def cut_text(e):
global selected
if e:
selected = root.clipboard_get()
else:
if text.selection_get():
selected = text.selection_get()
text.delete("sel.first", "sel.last")
root.clipboard_clear()
root.clipboard_append(selected)
def copy_text(e):
global selected
if e:
selected = root.clipboard_get()
if text.selection_get():
selected = text.selection_get()
root.clipboard_clear()
root.clipboard_append(selected)
def paste_text(e):
global selected
if e:
selected = root.clipboard_get()
else:
if selected:
position = text.index(INSERT)
text.insert(position, selected)
def Bold():
bold_font = font.Font(text, text.cget("font"))
bold_font.configure(weight="bold")
text.tag_configure("bold", font=bold_font)
current_tags = text.tag_names("sel.first")
if "bold" in current_tags:
text.tag_remove("bold", "sel.first", "sel.last")
else:
text.tag_add("bold", "sel.first", "sel.last")
def bg_color():
my_color = colorchooser.askcolor()[1]
if my_color:
text.config(bg=my_color)
def Italics():
italics_font = font.Font(text, text.cget("font"))
italics_font.configure(slant="italic")
text.tag_configure("italic", font=italics_font)
current_tags = text.tag_names("sel.first")
if "italic" in current_tags:
text.tag_remove("italic", "sel.first", "sel.last")
else:
text.tag_add("italic", "sel.first", "sel.last")
def text_color():
my_color = colorchooser.askcolor()[1]
if my_color:
status_bar.config(text=my_color)
color_font = font.Font(text, text.cget("font"))
color_font.configure()
text.tag_configure("colored", font=color_font, foreground=my_color)
current_tags = text.tag_names("sel.first")
if "colored" in current_tags:
text.tag_remove("colored", "sel.first", "sel.last")
else:
text.tag_add("colored", "sel.first", "sel.last")
def all_text_color():
my_color = colorchooser.askcolor()[1]
if my_color:
text.config(fg=my_color)
def print_file():
file_to_print = filedialog.askopenfilename(title="Open File", filetypes=[("Text Files", "*.txt")])
if file_to_print:
win32api.ShellExecute(0, "print", file_to_print, None, ".", 0)
def Dark_Mode():
main_color = "#000000"
second_color = "#373737"
text_color = "white"
root.config(bg=second_color)
status_bar.config(bg=second_color, fg=text_color)
text.config(bg=main_color, fg=text_color)
toolbar_frame.config(bg=second_color)
bold_button.config(bg=main_color, fg=text_color)
italics_button.config(bg=main_color, fg=text_color)
undo_button.config(bg=main_color, fg=text_color)
redo_button.config(bg=main_color, fg=text_color)
color_text_button.config(bg=main_color, fg=text_color)
file_menu.config(bg=second_color, fg=text_color)
edit_menu.config(bg=second_color, fg=text_color)
color_menu.config(bg=second_color, fg=text_color)
options_menu.config(bg=second_color, fg=text_color)
text.config(selectbackground="#ffe5b4")
def Light_Mode():
main_color = "SystemButtonFace"
second_color = "SystemButtonFace"
text_color = "black"
root.config(bg=second_color)
status_bar.config(bg=second_color, fg=text_color)
text.config(bg="white", fg="black")
toolbar_frame.config(bg=second_color)
bold_button.config(bg=main_color, fg=text_color)
italics_button.config(bg=main_color, fg=text_color)
undo_button.config(bg=main_color, fg=text_color)
redo_button.config(bg=main_color, fg=text_color)
color_text_button.config(bg=main_color, fg=text_color)
file_menu.config(bg=second_color, fg=text_color)
edit_menu.config(bg=second_color, fg=text_color)
color_menu.config(bg=second_color, fg=text_color)
options_menu.config(bg=second_color, fg=text_color)
text.config(selectbackground="gray")
def select_all(e):
text.tag_add("sel", "1.0", "end")
def clear_all():
text.delete(1.0, END)
toolbar_frame = Frame(root)
toolbar_frame.pack(fill=X)
frame = Frame(root)
frame.pack(pady=5)
text_scroll = Scrollbar(frame)
text_scroll.pack(side=RIGHT, fill=Y)
hor_scroll = Scrollbar(frame, orient="horizontal")
hor_scroll.pack(side=BOTTOM, fill=X)
text = Text(frame, width=98, height=25, font=("Helvetica", 16), selectbackground="gray", selectforeground="black", undo=True, yscrollcommand=text_scroll.set,xscrollcommand=hor_scroll.set, wrap="none")
text.pack()
text_scroll.config(command=text.yview)
hor_scroll.config(command=text.xview)
menu = Menu(root)
root.config(menu=menu)
file_menu = Menu(menu, tearoff=False)
menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New", command=New_file)
file_menu.add_command(label="Open", command = open_file)
file_menu.add_command(label="Save", command=save_file)
file_menu.add_command(label="Save As", command=save_as_file)
file_menu.add_separator()
file_menu.add_command(label="Print File", command=print_file)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=root.quit)
edit_menu = Menu(menu, tearoff=False)
menu.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="Cut", command=lambda: cut_text(False), accelerator="(Ctrl+x)")
edit_menu.add_command(label="Copy", command = lambda: copy_text(False), accelerator="(Ctrl+c)")
edit_menu.add_command(label="Paste ", command=lambda: paste_text(False), accelerator="(Ctrl+p)")
edit_menu.add_separator()
edit_menu.add_command(label="Undo ", command=text.edit_undo, accelerator="(Ctrl+z)")
edit_menu.add_command(label="Redo", command=text.edit_redo, accelerator="(Ctrl+y)")
edit_menu.add_separator()
edit_menu.add_command(label="Select All", command=lambda: select_all(True), accelerator="(Ctrl+a)")
edit_menu.add_command(label="Clear", command=clear_all, accelerator="(Delete)")
color_menu = Menu(menu, tearoff=False)
menu.add_cascade(label="Colors", menu=color_menu)
color_menu.add_command(label="Selected Text", command=text_color)
color_menu.add_command(label="All Text", command=all_text_color)
color_menu.add_command(label="Background", command=bg_color)
options_menu = Menu(menu, tearoff=False)
menu.add_cascade(label="Options", menu=options_menu)
options_menu.add_command(label="Dark Mode", command=Dark_Mode)
options_menu.add_command(label="Light Mode", command = Light_Mode)
status_bar = Label(root, text="Ready ", anchor=E)
status_bar.pack(fill=X, side=BOTTOM, ipady=15)
root.bind("<Control-Key-x>", cut_text)
root.bind("<Control-Key-c>", copy_text)
root.bind("<Control-Key-v>", paste_text)
root.bind("<Control-A>", select_all)
root.bind("<Control-a>", select_all)
bold_button = Button(toolbar_frame, text="Bold", command=Bold)
bold_button.grid(row=0, column=0, sticky=W, padx=5)
italics_button = Button(toolbar_frame, text="Italics", command=Italics)
italics_button.grid(row=0, column=1, padx=5)
undo_button = Button(toolbar_frame, text="Undo", command=text.edit_undo)
undo_button.grid(row=0, column=2, padx=5)
redo_button = Button(toolbar_frame, text="Redo", command=text.edit_redo)
redo_button.grid(row=0, column=3, padx=5)
color_text_button = Button(toolbar_frame, text="Text Color", command=text_color)
color_text_button.grid(row=0, column=4, padx=5)
root.mainloop()