-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
289 lines (227 loc) · 8.6 KB
/
main.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from tkinter import *
from tkinter import filedialog, font, ttk, scrolledtext, colorchooser
from gtts import gTTS
import os
from textblob import TextBlob
import speech_recognition as sr
root = Tk()
root.title("NotePad")
root.geometry('800x500')
root.resizable(0, 0)
language = 'en'
current_open_file = 'no file'
font_ = 'Consoles'
size = 10
weight = 'normal'
try:
def speech_to_text():
p = sr.Recognizer()
with sr.Microphone(device_index=1) as source:
print('Speak: ')
audio = p.listen(source)
try:
text = p.recognize_google(audio)
text_area.insert(END, text.capitalize())
s = text.split()
if s[0] in ('are', 'is', 'have', 'has', 'where',
'how', 'what', 'when', 'do', "don't", 'does', "doesn't"):
text_area.insert(END, '? ')
else:
text_area.insert(END, '. ')
except:
print("Cannot hear you")
except:
pass
def font_combobox(event):
global font_
font_ = font_box.get()
def size_combobox(event):
global size
size = size_box.get()
def style_combobox(event):
global weight
weight = style_box.get()
def font_window():
global font_box
global size_box
global style_box
window = Toplevel(root)
window.geometry('455x435')
window.title('Font')
font_tuple = font.families()
font_label = Label(window, text='Font:').place(x=5, y=2)
font_family = StringVar()
font_box = ttk.Combobox(window, width=22,
textvariable=font_family,
state='readonly',
height=12)
font_box['values'] = font_tuple
font_box.current(font_tuple.index('Arial'))
font_box.grid(row=0, column=0, padx=5, pady=5)
font_box.bind('<<ComboboxSelected>>', font_combobox)
style_label = Label(window, text='Font style:').place(x=170, y=2)
font_style = ('bold', 'normal')
font_style_var = StringVar()
style_box = ttk.Combobox(window, width=20,
textvariable=font_style_var,
state='readonly')
style_box['values'] = font_style
style_box.grid(row=0, column=1, padx=5, pady=5)
style_box.bind('<<ComboboxSelected>>', style_combobox)
size_label = Label(window, text='Size:').place(x=325, y=2)
size_tuple = (8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72)
size_ = IntVar()
size_box = ttk.Combobox(window, width=15,
textvariable=size_,
state='readonly',
height=12)
size_box['values'] = size_tuple
size_box.grid(row=0, column=2, padx=5, pady=20)
size_box.bind('<<ComboboxSelected>>', size_combobox)
def OK():
text_area.config(font=(font_, size, weight))
OK = Button(window, text='OK', command=OK).place(x=280, y=400, width=70)
Cancel = Button(window, text='Cancel', command=window.destroy)
Cancel.place(x=360, y=400, width=70)
def play():
try:
text_ = text_area.get(1.0, END)
audio = gTTS(text=text_, lang=language, slow=False)
audio.save('T22S.wav')
os.system('T22S.wav')
except AssertionError:
pass
def spell_check():
text_ = text_area.get(1.0, END)
corrected_text = TextBlob(text_)
text_area.delete(1.0, END)
for line in str(corrected_text.correct()):
text_area.insert(END, line)
def new_file():
global current_open_file
text_area.delete(1.0, END)
current_open_file = 'no_file'
def open_file():
global current_open_file
open_return = filedialog.askopenfile(initialdir='/',
title='select file to open',
filetypes=(('text files',
'*.txt'),
('all files',
'*.*')))
if open_return is not None:
text_area.delete(1.0, END)
for line in open_return:
text_area.insert(END, line)
current_open_file = open_return.name
open_return.close()
def save():
if current_open_file == 'no file':
save_as_file()
else:
with open(current_open_file, 'w+') as file:
file.write(text_area.get(1.0, END))
def save_as_file():
global current_open_file
file = filedialog.asksaveasfile(mode='w',
defaultextension='.txt')
if file is None:
return
text_to_save = text_area.get(1.0, END)
current_open_file = file.name
file.write(text_to_save)
file.close()
def copy_():
text_area.clipboard_clear()
text_area.clipboard_append(text_area.get(1.0, END))
def cut_():
text_area.delete(1.0, END)
def paste():
text_area.insert(INSERT, text_area.clipboard_get())
def redo():
try:
text_area.edit_redo()
except:
pass
def undo():
try:
text_area.edit_undo()
except:
pass
def text_color():
my_color = colorchooser.askcolor()[1]
if my_color:
color_font = font.Font(text_area, text_area.cget("font"))
text_area.tag_configure("colored", font=color_font, foreground=my_color)
try:
current_tags = text_area.tag_names("sel.first")
if "colored" in current_tags:
text_area.tag_remove("colored", "sel.first", "sel.last")
else:
text_area.tag_add("colored", "sel.first", "sel.last")
except:
print("Choose text")
def bg_color():
my_color = colorchooser.askcolor()[1]
if my_color:
text_area.config(bg=my_color)
def all_text_color():
my_color = colorchooser.askcolor()[1]
if my_color:
text_area.config(fg=my_color)
def night_on():
main_color = "#000000"
second_color = "#373737"
text_color = "Orange"
root.config(bg=main_color)
text_area.config(fg=text_color)
text_area.config(bg=second_color)
def night_off():
main_color = "SystemButtonFace"
second_color = "SystemButtonFace"
text_color = "black"
root.config(bg=main_color)
text_area.config(fg=text_color)
text_area.config(bg="white")
text_area = scrolledtext.ScrolledText(root, undo=True,
wrap=WORD, relief=FLAT,
width=600, height=400)
text_area.pack(fill=Y, expand=1)
main_menu = Menu()
root.config(menu=main_menu)
file_menu = Menu(main_menu, tearoff=False)
main_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_menu.add_command(label='Save as...', command=save_as_file)
file_menu.add_separator()
file_menu.add_command(label='Exit', command=root.destroy)
edit_menu = Menu(main_menu, tearoff=False)
main_menu.add_cascade(label='Edit', menu=edit_menu)
edit_menu.add_command(label='Undo', command=undo)
edit_menu.add_separator()
edit_menu.add_command(label='Redo', command=redo)
edit_menu.add_command(label='Copy', command=copy_)
edit_menu.add_command(label='Cut', command=cut_)
edit_menu.add_command(label='Paste', command=paste)
text_to_speech = Menu(main_menu, tearoff=False)
main_menu.add_cascade(label='Features', menu=text_to_speech)
text_to_speech.add_command(label='Text to Speech', command=play)
text_to_speech.add_separator()
text_to_speech.add_command(label='Spell Check', command=spell_check)
text_to_speech.add_separator()
text_to_speech.add_command(label='Speech to Text', command=speech_to_text)
font_menu = Menu(main_menu, tearoff=False)
main_menu.add_cascade(label="Format", menu=font_menu)
font_menu.add_command(label='Font...', command=font_window)
themes_menu = Menu(main_menu, tearoff=False)
main_menu.add_cascade(label="Themes", menu=themes_menu)
themes_menu.add_command(label="Night Mode On", command=night_on)
themes_menu.add_command(label="Night Mode Off", command=night_off)
color_menu = Menu(main_menu, tearoff=False)
main_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)
root.mainloop()