-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyPad.py
518 lines (428 loc) · 16.1 KB
/
PyPad.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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
"""
A Text Editor written in Python with some additional features other than the basic features of Notepad.
~ Ronik Bhattacharjee
"""
from tkinter import *
import tkinter.filedialog, tkinter.messagebox, tkinter.colorchooser, tkinter.font, tkinter.ttk
import pyautogui as pg
import datetime
from tkfontchooser import askfont
import webbrowser
from googletrans import Translator
from tkinter.scrolledtext import ScrolledText
import pyttsx3
import speech_recognition as sr
from threading import Thread
import re
def PyPad():
root = Tk()
root.title("Untitled - PyPad")
root.geometry("600x600")
# Setting icon of master window
icon = PhotoImage(file = 'ppad.png')
root.iconphoto(False, icon)
status_bar = Frame(master=root,bg="#ededed",height=30, highlightthickness=1, highlightbackground="#cecece")
status_bar.pack(side=BOTTOM,fill=X)
scrollbar_y = Scrollbar(root)
scrollbar_y.pack(side = RIGHT, fill = Y)
scrollbar_x = Scrollbar(root, orient='horizontal')
scrollbar_x.pack(side = BOTTOM, fill = X)
textfield = Text(root,
wrap = "none",
tabs=4,
yscrollcommand = scrollbar_y.set,
xscrollcommand = scrollbar_x.set,
undo=True, autoseparators=True,
maxundo=-1,
font=("Arial", 12))
textfield.pack(expand = True, fill=BOTH)
scrollbar_y.config(command = textfield.yview )
scrollbar_x.config(command = textfield.xview )
k = TextWidget(root, textfield)
########## Toolbar ##########
main_menu=Menu(root,bg="#cedbff",tearoff=0)
file_menu=Menu(main_menu,tearoff=0)
file_menu.add_command(label='New', command=k.new)
file_menu.add_command(label='Open', command=k.open_file)
file_menu.add_command(label='Save', command=k.save_file)
file_menu.add_command(label='Save As', command=k.save_file_as)
file_menu.add_separator()
file_menu.add_command(label='Exit All', command=k.exit)
edit_menu=Menu(main_menu,tearoff=0)
edit_menu.add_command(label='Cut', command=k.cut)
edit_menu.add_command(label='Copy', command=k.copy)
edit_menu.add_command(label='Paste', command=k.paste)
edit_menu.add_command(label='Delete', command=k.delete)
edit_menu.add_separator()
edit_menu.add_command(label='Undo', command=textfield.edit_undo)
edit_menu.add_command(label='Redo', command=textfield.edit_redo)
edit_menu.add_command(label='Select All', command=k.select_all)
edit_menu.add_separator()
edit_menu.add_command(label='Time/Date', command=k.timedate)
format_menu=Menu(main_menu,tearoff=0)
format_menu.add_command(label='Font Color', command=k.font_color)
format_menu.add_command(label='Font Style', command=k.font_style)
format_menu.add_separator()
format_menu.add_command(label='Word Wrap', command=k.word_wrap)
view_menu=Menu(main_menu,tearoff=0)
view_menu.add_command(label='Evaluate', command=k.evaluate)
view_menu.add_command(label='Translate', command=k.translate)
view_menu.add_separator()
view_menu.add_command(label='Text to Speech', command=k.text_to_speech)
view_menu.add_command(label='Speech to Text', command=k.speech_to_text)
view_menu.add_separator()
view_menu.add_command(label='Search on web', command=k.search_on_web)
view_menu.add_command(label='Goto URL', command=k.goto_url)
view_menu.add_separator()
view_menu.add_command(label='To UpperCase', command=k.to_upper_case)
view_menu.add_command(label='To LowerCase', command=k.to_lower_case)
view_menu.add_separator()
view_menu.add_command(label='Total Characters', command=k.char_len)
view_menu.add_command(label='Total Words', command=k.how_many_words)
main_menu.add_cascade(label='File', menu = file_menu)
main_menu.add_cascade(label='Edit', menu = edit_menu)
main_menu.add_cascade(label='Format', menu = format_menu)
main_menu.add_cascade(label='Options', menu = view_menu)
main_menu.add_command(label='About', command=k.about)
root.config(menu=main_menu)
# STATUS BAR
def i_r_c(text):
mtext = text.split(".")
t = "Line "+mtext[0]+", Col "+mtext[1]
return t
idx = Label(status_bar, text=i_r_c(textfield.index(tkinter.INSERT)))
idx.pack(side=RIGHT)
def update_statusbar(*args):
m = i_r_c(textfield.index(tkinter.INSERT))
idx["text"] = m
root.bind("<Key>", update_statusbar)
root.mainloop()
class TextWidget:
def __init__(self, root, text):
self.root = root
self.text = text
self.filename = ''
self._filetypes = [
('Text', '*.txt'),
('All files', '*'),
]
# SPELL CHECK
self.text.tag_configure("misspelled", foreground="red", underline=True)
self._words=open("./words_alpha.txt").read().split("\n") # Wordlist Link : https://github.com/dwyl/english-words
# LINK CHECK
self.text.tag_configure("url", foreground="blue", underline=True)
text.bind("<space>", lambda x: [self.Spellcheck(x), self.tag_links(x)])
def Spellcheck(self, event):
index = self.text.search(r'\s', "insert", backwards=True, regexp=True)
if index == "":
index ="1.0"
else:
index = self.text.index("%s+1c" % index)
word = self.text.get(index, "insert")
if word in self._words:
self.text.tag_remove("misspelled", index, "%s+%dc" % (index, len(word)))
else:
self.text.tag_add("misspelled", index, "%s+%dc" % (index, len(word)))
def tag_links(self, event):
index = self.text.search(r'\s', "insert", backwards=True, regexp=True)
if index == "":
index ="1.0"
else:
index = self.text.index("%s+1c" % index)
word = self.text.get(index, "insert")
regex = ("((http|https)://)(www.)?" +
"[a-zA-Z0-9@:%._\\+~#?&//=]" +
"{2,256}\\.[a-z]" +
"{2,6}\\b([-a-zA-Z0-9@:%" +
"._\\+~#?&//=]*)")
p = re.compile(regex)
if(re.search(p, word)):
self.text.tag_add("url", index, "%s+%dc" % (index, len(word)))
else:
self.text.tag_remove("url", index, "%s+%dc" % (index, len(word)))
def save_file(self):
if (self.filename == ''):
self.save_file_as()
else:
f = open(self.filename, 'w')
f.write(self.text.get('1.0', 'end-1c'))
f.close()
tkinter.messagebox.showinfo('PyPad', 'File Saved')
def save_file_as(self):
temp = tkinter.filedialog.asksaveasfilename(defaultextension='.txt', filetypes = self._filetypes)
if temp:
self.filename = temp
f = open(self.filename, 'w')
f.write(self.text.get('1.0', 'end-1c'))
f.close()
self.root.title(f"{self.filename} - PyPad")
tkinter.messagebox.showinfo('PyPad', 'File Saved')
def open_file(self, filename = None):
if not filename:
self.filename = tkinter.filedialog.askopenfilename(filetypes = self._filetypes)
else:
self.filename = filename
if not (self.filename == ''):
f = open(self.filename, 'r')
f2 = f.read()
self.text.delete('1.0', 'end')
self.text.insert('1.0', f2)
f.close()
self.root.title(f"{self.filename} - PyPad")
def new(self):
PyPad()
def exit(self):
exit()
def cut(self):
pg.hotkey('ctrl','x')
def copy(self):
pg.hotkey('ctrl','c')
def paste(self):
pg.hotkey('ctrl','v')
def select_all(self):
pg.hotkey('ctrl','a')
def delete(self):
pg.press('delete')
def timedate(self):
tad = datetime.datetime.now()
self.text.insert("insert", tad)
def font_color(self):
new_color = tkinter.colorchooser.askcolor()[1]
self.text['fg'] = new_color
def font_style(self):
font = askfont(self.root)
if font:
font['family'] = font['family'].replace(' ', '\ ')
font_str = "%(family)s %(size)i %(weight)s %(slant)s" % font
if font['underline']:
font_str += ' underline'
if font['overstrike']:
font_str += ' overstrike'
self.text["font"] = font_str
def evaluate(self):
try:
sel = self.text.selection_get()
try:
value = eval(sel)
tkinter.messagebox.showinfo('PyPad', f'The answer is : {value}')
except Exception as e:
tkinter.messagebox.showinfo('PyPad', "Invalid expression")
except:
tkinter.messagebox.showinfo('PyPad', 'You need to select something to evaluate')
def translate(self):
sel = None
def translation_opt():
t = opt_var.get()
translator = Translator(service_urls=['translate.googleapis.com'])
translation=translator.translate(var2.get(),dest=t)
textbox.replace("1.0", END, translation.text)
# var2.set(translation.text)
lang_list = [
'afrikaans',
'albanian',
'amharic',
'arabic',
'armenian',
'azerbaijani',
'basque',
'belarusian',
'bengali',
'bosnian',
'bulgarian',
'catalan',
'cebuano',
'chichewa',
'chinese (simplified)',
'chinese (traditional)',
'corsican',
'croatian',
'czech',
'danish',
'dutch',
'english',
'esperanto',
'estonian',
'filipino',
'finnish',
'french',
'frisian',
'galician',
'georgian',
'german',
'greek',
'gujarati',
'haitian creole',
'hausa',
'hawaiian',
'hebrew',
'hebrew',
'hindi',
'hmong',
'hungarian',
'icelandic',
'igbo',
'indonesian',
'irish',
'italian',
'japanese',
'javanese',
'kannada',
'kazakh',
'khmer',
'korean',
'kurdish (kurmanji)',
'kyrgyz',
'lao',
'latin',
'latvian',
'lithuanian',
'luxembourgish',
'macedonian',
'malagasy',
'malay',
'malayalam',
'maltese',
'maori',
'marathi',
'mongolian',
'myanmar (burmese)',
'nepali',
'norwegian',
'odia',
'pashto',
'persian',
'polish',
'portuguese',
'punjabi',
'romanian',
'russian',
'samoan',
'scots gaelic',
'serbian',
'sesotho',
'shona',
'sindhi',
'sinhala',
'slovak',
'slovenian',
'somali',
'spanish',
'sundanese',
'swahili',
'swedish',
'tajik',
'tamil',
'telugu',
'thai',
'turkish',
'ukrainian',
'urdu',
'uyghur',
'uzbek',
'vietnamese',
'welsh',
'xhosa',
'yiddish',
'yoruba',
'zulu',
]
try:
sel = self.text.selection_get()
except:
tkinter.messagebox.showinfo('PyPad', 'You need to select something to translate')
if sel:
try:
master = Toplevel(self.root)
master.geometry("600x600")
master.title("Translation - PyPad")
master["bg"] = "#fff"
tools_area = Frame(master=master,bg="#1F7DFF",width=100)
tools_area.pack(side=LEFT,fill=Y)
var2 = StringVar() # to be translated
var2.set(sel)
opt_var = StringVar(master)
opt_var.set('Hindi') # default option
opt_frame = LabelFrame(master=tools_area,bg="#1F7DFF",fg="#fff",text="Select Language",labelanchor='n',relief=GROOVE,bd=1)
opt_frame.pack(anchor="center",padx=10,pady=20)
e = tkinter.ttk.Combobox(opt_frame, textvariable=opt_var, values=lang_list)
e.pack(anchor="center",padx=10,pady=10)
textbox = ScrolledText(master, bg="#fff", wrap = tkinter.WORD)
textbox.pack(expand = True, fill=BOTH)
textbox.insert("insert", var2.get())
# textbox = Label(master, textvariable=var2, bg="#fff").pack(expand = True, fill=BOTH)
Button(tools_area, width=8, height=1,relief=FLAT, text='Translate',command=translation_opt).pack(padx=12,pady=10,anchor="center", side=BOTTOM)
master.mainloop()
except Exception as e:
tkinter.messagebox.showinfo('PyPad', e)
def text_to_speech(self):
try:
sel = self.text.selection_get()
engine = pyttsx3.init()
engine.say(sel)
engine.runAndWait()
except Exception as e:
tkinter.messagebox.showinfo('PyPad', "Select something to convert to speech")
def speech_to_text(self):
def capture():
rec = sr.Recognizer()
with sr.Microphone() as source:
audio = rec.listen(source, phrase_time_limit=5)
try:
text = rec.recognize_google(audio, language='en-US')
return text
except:
return 0
while 1:
captured_text = capture().lower()
if captured_text == 0:
continue
if 'quit' in str(captured_text):
break
self.text.insert("insert", captured_text)
def search_on_web(self):
try:
sel = self.text.selection_get()
webbrowser.open(f'https://www.google.com/search?q={sel}')
except:
tkinter.messagebox.showinfo('PyPad', 'You need to select something to search on web')
def goto_url(self):
try:
sel = self.text.selection_get()
webbrowser.open_new(sel)
except:
tkinter.messagebox.showinfo('PyPad', 'Select a link. Valid links will be underlined and highlighted in blue.')
def to_upper_case(self):
try:
sel = self.text.selection_get()
self.text.delete(SEL_FIRST, SEL_LAST)
self.text.insert("insert", sel.upper())
except Exception as e:
tkinter.messagebox.showinfo('PyPad', "Select something")
def to_lower_case(self):
try:
sel = self.text.selection_get()
self.text.delete(SEL_FIRST, SEL_LAST)
self.text.insert("insert", sel.lower())
except Exception as e:
tkinter.messagebox.showinfo('PyPad', "Select something")
def char_len(self):
try:
sel = self.text.selection_get()
tkinter.messagebox.showinfo('PyPad', "Total Characters: " + str(len(sel)))
except Exception as e:
tkinter.messagebox.showinfo('PyPad', "Select something")
def how_many_words(self):
try:
sel = self.text.selection_get()
tkinter.messagebox.showinfo('PyPad', "Word Count: " + str(len(sel.split())))
except Exception as e:
tkinter.messagebox.showinfo('PyPad', "Select something")
def word_wrap(self):
if self.text["wrap"] == "none":
self.text["wrap"] = "word"
else:
self.text["wrap"] = "none"
def about(self):
tkinter.messagebox.showinfo('PyPad', ' A Text Editor written in Python with some additional features other than the basic features of Notepad.\n\n~ Made by Ronik Bhattacharjee')
if __name__ == "__main__":
PyPad()