forked from ritesh1601/Text-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.py
More file actions
425 lines (342 loc) · 16.8 KB
/
editor.py
File metadata and controls
425 lines (342 loc) · 16.8 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import tkinter as tk
from tkinter import ttk, font, colorchooser, filedialog, messagebox
import os
import speech_recognition as sr
main_application = tk.Tk()
main_application.geometry('1200x800')
main_application.title('Vpad text editor')
main_application.wm_iconbitmap('icon.ico')
############################################## Main Menu ###################################################
main_menu = tk.Menu()
# File icons
new_icon = tk.PhotoImage(file='icons2/new.png')
open_icon = tk.PhotoImage(file='icons2/open.png')
save_icon = tk.PhotoImage(file='icons2/save.png')
save_as_icon = tk.PhotoImage(file='icons2/save_as.png')
exit_icon = tk.PhotoImage(file='icons2/exit.png')
file = tk.Menu(main_menu, tearoff=False)
# Edit icons
copy_icon = tk.PhotoImage(file='icons2/copy.png')
paste_icon = tk.PhotoImage(file='icons2/paste.png')
cut_icon = tk.PhotoImage(file='icons2/cut.png')
clear_all_icon = tk.PhotoImage(file='icons2/clear_all.png')
find_icon = tk.PhotoImage(file='icons2/find.png')
edit = tk.Menu(main_menu, tearoff=False)
# View icons
tool_bar_icon = tk.PhotoImage(file='icons2/tool_bar.png')
status_bar_icon = tk.PhotoImage(file='icons2/status_bar.png')
view = tk.Menu(main_menu, tearoff=False)
# Color theme
light_default_icon = tk.PhotoImage(file='icons2/light_default.png')
light_plus_icon = tk.PhotoImage(file='icons2/light_plus.png')
dark_icon = tk.PhotoImage(file='icons2/dark.png')
red_icon = tk.PhotoImage(file='icons2/red.png')
monokai_icon = tk.PhotoImage(file='icons2/monokai.png')
night_blue_icon = tk.PhotoImage(file='icons2/night_blue.png')
color_theme = tk.Menu(main_menu, tearoff=False)
theme_choice = tk.StringVar()
color_icons = (light_default_icon, light_plus_icon, dark_icon, red_icon, monokai_icon, night_blue_icon)
color_dict = {
'Light Default ': ('#000000', '#ffffff'),
'Light Plus': ('#474747', '#e0e0e0'),
'Dark': ('#c4c4c4', '#2d2d2d'),
'Red': ('#2d2d2d', '#ffe8e8'),
'Monokai': ('#d3b774', '#474747'),
'Night Blue': ('#ededed', '#6b9dc2')
}
# Cascade
main_menu.add_cascade(label='File', menu=file)
main_menu.add_cascade(label='Edit', menu=edit)
main_menu.add_cascade(label='View', menu=view)
main_menu.add_cascade(label='Color Theme', menu=color_theme)
############################################## Toolbar ###################################################
tool_bar = ttk.Label(main_application)
tool_bar.pack(side=tk.TOP, fill=tk.X)
# Font box
font_tuple = tk.font.families()
font_family = tk.StringVar()
font_box = ttk.Combobox(tool_bar, width=30, textvariable=font_family, state='readonly', cursor="hand")
font_box['values'] = font_tuple
font_box.current(font_tuple.index('Arial'))
font_box.grid(row=0, column=0, padx=5)
# Size box
size_var = tk.IntVar()
font_size = ttk.Combobox(tool_bar, width=14, textvariable=size_var, state='readonly', cursor="hand")
font_size['values'] = tuple(range(8, 81))
font_size.current(4)
font_size.grid(row=0, column=1, padx=5)
# Bold button
bold_icon = tk.PhotoImage(file='icons2/bold.png').subsample(2, 2)
bold_btn = ttk.Button(tool_bar, image=bold_icon , cursor="hand")
bold_btn.grid(row=0, column=2, padx=5)
# Italic button
italic_icon = tk.PhotoImage(file='icons2/italic.png').subsample(2, 2)
italic_btn = ttk.Button(tool_bar, image=italic_icon , cursor="hand")
italic_btn.grid(row=0, column=3, padx=5)
# Underline button
underline_icon = tk.PhotoImage(file='icons2/underline.png').subsample(2, 2)
underline_btn = ttk.Button(tool_bar, image=underline_icon , cursor="hand")
underline_btn.grid(row=0, column=4, padx=5)
# Font color button
font_color_icon = tk.PhotoImage(file='icons2/font_color.png').subsample(2, 2)
font_color_btn = ttk.Button(tool_bar, image=font_color_icon , cursor="hand")
font_color_btn.grid(row=0, column=5, padx=5)
# Align left button
align_left_icon = tk.PhotoImage(file='icons2/align_left.png').subsample(2, 2)
align_left_btn = ttk.Button(tool_bar, image=align_left_icon , cursor="hand")
align_left_btn.grid(row=0, column=6, padx=5)
# Align center button
align_center_icon = tk.PhotoImage(file='icons2/align_center.png').subsample(2, 2)
align_center_btn = ttk.Button(tool_bar, image=align_center_icon , cursor="hand")
align_center_btn.grid(row=0, column=7, padx=5)
# Align right button
align_right_icon = tk.PhotoImage(file='icons2/align_right.png').subsample(2, 2)
align_right_btn = ttk.Button(tool_bar, image=align_right_icon , cursor="hand")
align_right_btn.grid(row=0, column=8, padx=5)
# Bold italic button
bold_italic_icon = tk.PhotoImage(file='icons2/Bold+Italic.png').subsample(29, 29)
bold_italic_btn = ttk.Button(tool_bar, image=bold_italic_icon , cursor="hand")
bold_italic_btn.grid(row=0, column=9, padx=5)
# Speech to text button
speech_to_text_icon = tk.PhotoImage(file='icons2/speech2.png').subsample(29, 29)
speech_to_text_btn = ttk.Button(tool_bar, image=speech_to_text_icon , cursor="hand")
speech_to_text_btn.grid(row=0, column=10, padx=5)
# -------------------------------------&&&&&&&& End Toolbar &&&&&&&&&&& ----------------------------------
############################################## Text Editor ###################################################
text_editor = tk.Text(main_application)
text_editor.config(wrap='word', relief=tk.FLAT)
scroll_bar = tk.Scrollbar(main_application)
text_editor.focus_set()
scroll_bar.pack(side=tk.RIGHT, fill=tk.Y)
text_editor.pack(fill=tk.BOTH, expand=True)
scroll_bar.config(command=text_editor.yview)
text_editor.config(yscrollcommand=scroll_bar.set)
# Font family and font size functionality
current_font_family = 'Arial'
current_font_size = 25
def change_font(event=None):
global current_font_family
current_font_family = font_family.get()
text_editor.configure(font=(current_font_family, current_font_size))
def change_fontsize(event=None):
global current_font_size
current_font_size = size_var.get()
text_editor.configure(font=(current_font_family, current_font_size))
font_box.bind("<<ComboboxSelected>>", change_font)
font_size.bind("<<ComboboxSelected>>", change_fontsize)
# Bold button functionality
def toggle_bold(event=None):
text_property = tk.font.Font(font=text_editor['font'])
if text_property.actual()['weight'] == 'normal':
text_editor.tag_add('bold', 'sel.first', 'sel.last')
text_editor.tag_configure('bold', font=(current_font_family, current_font_size, 'bold'))
elif text_property.actual()['weight'] == 'bold':
text_editor.tag_remove('bold', 'sel.first', 'sel.last')
text_editor.tag_configure('bold', font=(current_font_family, current_font_size, 'normal'))
bold_btn.configure(command=toggle_bold)
# Italic button functionality
def toggle_italic(event=None):
text_property = tk.font.Font(font=text_editor['font'])
if text_property.actual()['slant'] == 'roman':
text_editor.tag_add('italic', 'sel.first', 'sel.last')
text_editor.tag_configure('italic', font=(current_font_family, current_font_size, 'italic'))
elif text_property.actual()['slant'] == 'italic':
text_editor.tag_remove('italic', 'sel.first', 'sel.last')
text_editor.tag_configure('italic', font=(current_font_family, current_font_size, 'roman'))
italic_btn.configure(command=toggle_italic)
# Underline button functionality
def toggle_underline(event=None):
text_property = tk.font.Font(font=text_editor['font'])
if text_property.actual()['underline'] == 0:
text_editor.tag_add('underline', 'sel.first', 'sel.last')
text_editor.tag_configure('underline', underline=True)
elif text_property.actual()['underline'] == 1:
text_editor.tag_remove('underline', 'sel.first', 'sel.last')
text_editor.tag_configure('underline', underline=False)
underline_btn.configure(command=toggle_underline)
# Font color button functionality
def change_font_color(event=None):
color_var = tk.colorchooser.askcolor()
text_editor.configure(fg=color_var[1])
font_color_btn.configure(command=change_font_color)
# Align left button functionality
def align_left(event=None):
text_editor.tag_configure('center', justify=tk.CENTER)
text_editor.tag_configure('right', justify=tk.RIGHT)
text_editor.tag_remove('center', 'sel.first', 'sel.last')
text_editor.tag_remove('right', 'sel.first', 'sel.last')
text_editor.tag_configure('left', justify=tk.LEFT)
text_editor.tag_add('left', 'sel.first', 'sel.last')
align_left_btn.configure(command=align_left)
# Align center button functionality
def align_center(event=None):
text_editor.tag_configure('left', justify=tk.LEFT)
text_editor.tag_configure('right', justify=tk.RIGHT)
text_editor.tag_remove('left', 'sel.first', 'sel.last')
text_editor.tag_remove('right', 'sel.first', 'sel.last')
text_editor.tag_configure('center', justify=tk.CENTER)
text_editor.tag_add('center', 'sel.first', 'sel.last')
align_center_btn.configure(command=align_center)
# Align right button functionality
def align_right(event=None):
text_editor.tag_configure('left', justify=tk.LEFT)
text_editor.tag_configure('center', justify=tk.CENTER)
text_editor.tag_remove('left', 'sel.first', 'sel.last')
text_editor.tag_remove('center', 'sel.first', 'sel.last')
text_editor.tag_configure('right', justify=tk.RIGHT)
text_editor.tag_add('right', 'sel.first', 'sel.last')
align_right_btn.configure(command=align_right)
# Bold italic button functionality
def toggle_bold_italic(event=None):
text_property = tk.font.Font(font=text_editor['font'])
if text_property.actual()['weight'] == 'normal' and text_property.actual()['slant'] == 'roman':
text_editor.tag_add('bold_italic', 'sel.first', 'sel.last')
text_editor.tag_configure('bold_italic', font=(current_font_family, current_font_size, 'bold italic'))
else:
text_editor.tag_remove('bold_italic', 'sel.first', 'sel.last')
text_editor.tag_configure('bold_italic', font=(current_font_family, current_font_size, 'normal'))
# Bold and italic buttons
bold_btn.configure(command=toggle_bold_italic)
italic_btn.configure(command=toggle_bold_italic)
#bold and italic buttons separated
# Bold button functionality
def toggle_bold(event=None):
text_property = tk.font.Font(font=text_editor['font'])
if text_property.actual()['weight'] == 'normal':
text_editor.tag_add('bold', 'sel.first', 'sel.last')
text_editor.tag_configure('bold', font=(current_font_family, current_font_size, 'bold'))
elif text_property.actual()['weight'] == 'bold':
text_editor.tag_remove('bold', 'sel.first', 'sel.last')
text_editor.tag_configure('bold', font=(current_font_family, current_font_size, 'normal'))
bold_btn.configure(command=toggle_bold)
# Italic button functionality
def toggle_italic(event=None):
text_property = tk.font.Font(font=text_editor['font'])
if text_property.actual()['slant'] == 'roman':
text_editor.tag_add('italic', 'sel.first', 'sel.last')
text_editor.tag_configure('italic', font=(current_font_family, current_font_size, 'italic'))
elif text_property.actual()['slant'] == 'italic':
text_editor.tag_remove('italic', 'sel.first', 'sel.last')
text_editor.tag_configure('italic', font=(current_font_family, current_font_size, 'roman'))
italic_btn.configure(command=toggle_italic)
# Bold italic button functionality
def toggle_bold_italic(event=None):
text_property = tk.font.Font(font=text_editor['font'])
if text_property.actual()['weight'] == 'normal' and text_property.actual()['slant'] == 'roman':
text_editor.tag_add('bold_italic', 'sel.first', 'sel.last')
text_editor.tag_configure('bold_italic', font=(current_font_family, current_font_size, 'bold italic'))
else:
text_editor.tag_remove('bold_italic', 'sel.first', 'sel.last')
text_editor.tag_configure('bold_italic', font=(current_font_family, current_font_size, 'normal'))
bold_italic_btn.configure(command=toggle_bold_italic)
# Speech to text functionality
def speech_to_text():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
recognizer.adjust_for_ambient_noise(source)
print("Listening...")
try:
audio = recognizer.listen(source, timeout=5) # Set a timeout to stop listening after 5 seconds of silence
print("Stopped listening...")
recognized_text = recognizer.recognize_google(audio)
text_editor.insert(tk.END, recognized_text)
except sr.WaitTimeoutError:
print("No speech detected within the timeout period")
except sr.UnknownValueError:
print("Speech recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
speech_to_text_btn.configure(command=speech_to_text)
# -------------------------------------&&&&&&&& End Text Editor &&&&&&&&&&& ----------------------------------
############################################## Status Bar ###################################################
status_bar = ttk.Label(main_application, text='Status Bar')
status_bar.pack(side=tk.BOTTOM)
text_changed = False
def update_status_bar(event=None):
global text_changed
if text_editor.edit_modified():
text_changed = True
words = len(text_editor.get(1.0, 'end-1c').split())
characters = len(text_editor.get(1.0, 'end-1c'))
status_bar.config(text=f'Characters: {characters} Words: {words}')
text_editor.edit_modified(False)
text_editor.bind('<<Modified>>', update_status_bar)
# -------------------------------------&&&&&&&& End Status Bar &&&&&&&&&&& ----------------------------------
############################################## Main Menu Functionality ###################################################
# New file functionality
def new_file(event=None):
global url
url = ''
text_editor.delete(1.0, tk.END)
file.add_command(label='New', image=new_icon, compound=tk.LEFT, accelerator='Ctrl+N', command=new_file)
# Open file functionality
def open_file(event=None):
global url
url = filedialog.askopenfilename(initialdir=os.getcwd(), title='Select File', filetypes=(('Text File', '*.txt'), ('All files', '*.*')))
try:
with open(url, 'r') as fr:
text_editor.delete(1.0, tk.END)
text_editor.insert(1.0, fr.read())
except FileNotFoundError:
return
except:
return
main_application.title(os.path.basename(url))
file.add_command(label='Open', image=open_icon, compound=tk.LEFT, accelerator='Ctrl+O', command=open_file)
# Save file functionality
def save_file(event=None):
global url
try:
if url:
content = str(text_editor.get(1.0, tk.END))
with open(url, 'w', encoding='utf-8') as fw:
fw.write(content)
else:
url = filedialog.asksaveasfile(mode='w', defaultextension='.txt', filetypes=(('Text File', '*.txt'), ('All files', '*.*')))
content2 = text_editor.get(1.0, tk.END)
url.write(content2)
url.close()
except:
return
file.add_command(label='Save', image=save_icon, compound=tk.LEFT, accelerator='Ctrl+S', command=save_file)
# Save as functionality
def save_as(event=None):
global url
try:
content = text_editor.get(1.0, tk.END)
url = filedialog.asksaveasfile(mode='w', defaultextension='.txt', filetypes=(('Text File', '*.txt'), ('All files', '*.*')))
url.write(content)
url.close()
except:
return
file.add_command(label='Save As', image=new_icon, compound=tk.LEFT, accelerator='Ctrl+Alt+S', command=save_as)
# Exit functionality
def exit_func(event=None):
global url, text_changed
try:
if text_changed:
mbox = messagebox.askyesnocancel('Warning', 'Do you want to save the file?')
if mbox is True:
if url:
content = text_editor.get(1.0, tk.END)
with open(url, 'w', encoding='utf-8') as fw:
fw.write(content)
main_application.destroy()
else:
content2 = str(text_editor.get(1.0, tk.END))
url = filedialog.asksaveasfile(mode='w', defaultextension='.txt', filetypes=(('Text File', '*.txt'), ('All files', '*.*')))
url.write(content2)
url.close()
main_application.destroy()
elif mbox is False:
main_application.destroy()
else:
main_application.destroy()
except:
return
file.add_command(label='Exit', image=exit_icon, compound=tk.LEFT, accelerator='Ctrl+Q', command=exit_func)
# -------------------------------------&&&&&&&& End Main Menu Functionality &&&&&&&&&&& ----------------------------------
############################################## Bindings ###################################################
main_application.config(menu=main_menu)
############################################## Main Loop ###################################################
main_application.mainloop()