-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
403 lines (357 loc) · 14.7 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
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
# Author :- Aryan Khandelwal
"""
This is a simple Text Editor
Made with Tkinter Module in Python
"""
import tkinter as tk
from tkinter import ttk
from tkinter import *
from functools import partial
from tkinter import messagebox
from tkinter import filedialog
from tkinter import simpledialog
import os
import pyttsx3
import enchant
from ttkthemes import ThemedTk
import pastebin
class MyScroll(Text):
"""
Class used to Make a TextWidget
having horizontal and vertical
scrollbars
"""
def __init__(self, master=None, **kw):
self.frame = ttk.Frame(master)
self.vbar = ttk.Scrollbar(self.frame, command=self.yview)
self.vbar.pack(side=RIGHT, fill=Y)
self.hbar = ttk.Scrollbar(
self.frame, orient="horizontal", command=self.xview)
self.hbar.pack(side=BOTTOM, fill=X)
kw.update({'yscrollcommand': self.vbar.set})
kw.update({'xscrollcommand': self.hbar.set})
Text.__init__(self, self.frame, **kw)
self.pack(side=LEFT, fill=BOTH, expand=True)
text_meths = vars(Text).keys()
methods = vars(Pack).keys() | vars(Grid).keys() | vars(Place).keys()
methods = methods.difference(text_meths)
for m in methods:
if m[0] != '_' and m != 'config' and m != 'configure':
setattr(self, m, getattr(self.frame, m))
def __str__(self):
return str(self.frame)
class Editor:
"""
Base Class of Editor Made using Tkinter
"""
window = ThemedTk()
style = ttk.Style()
window.title("Notepad")
window.geometry(
"{0}x{1}+0+0".format(window.winfo_screenwidth(),
window.winfo_screenheight()))
menuBar = Menu(window)
# the text and entry frames column
window.grid_columnconfigure(1, weight=1)
window.grid_rowconfigure(0, weight=1)
# window.grid_rowconfigure(1, weight=1) # all frames row
window.config(menu=menuBar)
fileMenu = Menu(menuBar, tearoff=0)
editMenu = Menu(menuBar, tearoff=0)
viewMenu = Menu(menuBar, tearoff=0)
helpMenu = Menu(menuBar, tearoff=0)
txt = MyScroll(window, undo=True)
txt.grid(row=0, column=1, sticky="NSEW")
lineNumber = Canvas(window, width="30", height="500")
lineNumber.grid(row=0, column=0, sticky='NS', pady=1, rowspan=3)
wordCount = StringVar()
wordCount.set("Word Count -> 0")
statusBar = ttk.Label(window, textvariable=wordCount)
statusBar.grid(row=2, column=1, columnspan=2, sticky="EW")
txt['wrap'] = 'none'
fontType = "Calibre"
fontSize = "10"
fontColor = "black"
txt.config(font=str(fontType + ' ' + fontSize))
currentFile = "No File"
def __init__(self):
self.fileMenu.add_command(
label="New", command=self.new_file, accelerator="Ctrl+N")
self.window.bind_all('<Control-n>', self.new_file)
self.fileMenu.add_command(
label="Open", command=self.open_file, accelerator="Ctrl+O")
self.window.bind_all('<Control-o>', self.open_file)
self.fileMenu.add_command(
label="Save", command=self.save_file, accelerator="Ctrl+S")
self.window.bind_all('<Control-s>', self.save_file)
self.fileMenu.add_command(
label="Save As", command=self.save_file_as,
accelerator="Ctrl+Shift+S")
self.window.bind_all('<Control-S>', self.save_file_as)
self.fileMenu.add_command(label="Exit", command=self.exit)
self.menuBar.add_cascade(label="File", menu=self.fileMenu)
self.editMenu = Menu(self.menuBar, tearoff=0)
self.editMenu.add_command(label="Cut", command=self.cut)
self.editMenu.add_command(
label="Copy", command=self.copy, accelerator="Ctrl+C")
# self.window.bind_all('<Control-c>', self.copy)
self.editMenu.add_command(
label="Paste", command=self.paste, accelerator="Ctrl+V")
# self.window.bind_all('<Control-v>', self.paste)
self.editMenu.add_command(
label="Undo", command=self.undo, accelerator="Ctrl+Z")
self.window.bind_all('<Control-z>', self.undo)
self.editMenu.add_command(
label="Redo", command=self.redo, accelerator="Ctrl+R")
self.window.bind_all('<Control-r>', self.redo)
self.editMenu.add_command(
label="Find", command=self.find, accelerator="Ctrl+F")
self.window.bind_all('<Control-f>', self.find)
self.viewMenu.add_command(label="Font Size", command=self.font_size)
self.viewMenu.add_command(
label="Paste on pastebin", command=self.paste_on)
self.viewMenu.add_command(label="Speak It", command=self.speak)
self.viewMenu.add_command(
label="Spell Check", command=self.spell_check)
self.editMenu.add_command(
label="Replace All", command=self.replace,
accelerator="Ctrl+Shift+R")
self.window.bind_all('<Control-R>', self.replace)
self.menuBar.add_cascade(label="Edit", menu=self.editMenu)
self.menuBar.add_cascade(label="View", menu=self.viewMenu)
self.helpMenu.add_command(label="About", command=self.about)
self.menuBar.add_cascade(label="Help", menu=self.helpMenu)
self.window.bind_all('<Return>', self.redraw)
self.window.bind_all('<BackSpace>', self.redraw)
self.window.bind_all('<Key>', self.redraw)
self.window.bind_all('<Button-4>', self.redraw)
self.window.bind_all('<Button-5>', self.redraw)
self.window.bind_all('<Configure>', self.redraw)
self.window.bind_all('<Motion>', self.redraw)
self.editMenu.add_command(
label="Select All", command=self.selectall, accelerator="Ctrl+A")
self.window.bind_all('<Control-a>', self.selectall)
themeBar = Menu(self.viewMenu)
themeBar.add_command(label="Blacko", command=partial(
self.change_theme, "blacko"))
themeBar.add_command(label="Whity", command=partial(
self.change_theme, "whity"))
themeBar.add_command(label="Gyona", command=partial(
self.change_theme, "gyona"))
self.viewMenu.add_cascade(label="Themes", menu=themeBar)
self.window.mainloop()
def new_file(self, event=None):
if(messagebox.askyesno("Save?", "Do you wish to save current file?")):
self.save_file()
self.txt.delete('1.0', END)
self.window.title("Notepad")
self.currentFile = "No File"
else:
self.txt.delete('1.0', END)
self.window.title("Notepad")
self.currentFile = "No File"
def open_file(self, event=None):
print("opening file")
myFile = filedialog.askopenfile(
parent=self.window, mode="rb", title="My New File")
if myFile is not None:
self.window.title(os.path.basename(myFile.name))
content = myFile.read()
self.txt.delete('1.0', END)
self.txt.insert(1.0, content)
self.currentFile = myFile.name
myFile.close()
self.redraw(event)
def save_file_as(self, event=None):
print("saving file")
myFile = filedialog.asksaveasfile(mode="w")
if myFile is not None:
myFile.write(self.txt.get('1.0', END))
self.currentFile = myFile.name
myFile.close()
self.window.title(os.path.basename(myFile.name))
def save_file(self, event=None):
print(self.currentFile)
if (self.currentFile == "No File"):
self.save_file_as(event)
else:
myFile = open(self.currentFile, "w")
myFile.write(self.txt.get('1.0', END))
myFile.close()
def copy(self):
# print("copying")
self.txt.clipboard_clear()
self.txt.clipboard_append(self.txt.selection_get())
def cut(self):
self.copy()
self.txt.delete(SEL_FIRST, SEL_LAST)
def paste(self):
self.txt.insert(INSERT, self.txt.clipboard_get())
self.redraw(event)
def undo(self, event=None):
self.txt.edit_undo()
def redo(self, event=None):
self.txt.edit_redo()
def find(self, event=None):
root = Toplevel(self.window)
root.title("Find")
root.transient(self.window)
root.focus_force()
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
e1 = ttk.Entry(root)
e1.grid(row=0, column=0, pady="10",
padx="10", columnspan=2, sticky="EW")
def sub():
findString = e1.get()
self.set_mark(findString)
def on_closing():
self.txt.tag_delete('highlight')
root.destroy()
findBtn = ttk.Button(root, text="Find", command=sub)
findBtn.grid(row=1, column=0, pady="10", padx="10", sticky="EWS")
closeBtn = ttk.Button(root, text="Close", command=on_closing)
closeBtn.grid(row=1, column=1, pady="10", padx="10", sticky="EWS")
root.protocol("WM_DELETE_WINDOW", on_closing)
def paste_on(self, event=None):
def copy_link(self, link):
self.txt.clipboard_clear()
self.txt.clipboard_append(link)
root = Toplevel(self.window)
root.title("Link")
root.transient(self.window)
root.focus_force()
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
link = pastebin.pastebin(self.txt.get('1.0', END))
lb = ttk.Label(root, text=link)
lb.grid(row=0, column=0, padx="50", pady="20")
bt = ttk.Button(root, text="Copy", command=copy_link(self, link))
bt.grid(row=1, column=0, padx="50", pady="20")
def selectall(self, event=None):
self.txt.tag_add('sel', '1.0', 'end')
return "break"
def set_mark(self, findString):
print("Coming to set mark")
self.find_string(findString)
self.txt.tag_config('highlight', foreground='red')
self.txt.focus_force()
def find_string(self, findString):
startInd = '1.0'
while(startInd):
startInd = self.txt.search(findString, startInd, stopindex=END)
if startInd:
startInd = str(startInd)
lastInd = startInd+f'+{len(findString)}c'
print(startInd, lastInd)
self.txt.tag_add('highlight', startInd, lastInd)
startInd = lastInd
def replace(self, event=None):
print("coming replace")
root = Toplevel(self.window)
root.title("Find and Replace")
root.transient(self.window)
root.focus_force()
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
e1 = ttk.Entry(root)
e1.grid(row=0, column=0, pady=5, columnspan=2, padx=10)
e2 = ttk.Entry(root)
e2.grid(row=1, column=0, pady=5, columnspan=2, padx=10)
def find():
findString = e1.get()
self.set_mark(findString)
def replace():
findString = e1.get()
replaceString = e2.get()
myText = self.txt.get('1.0', END)
myText = myText.replace(findString, replaceString)
self.txt.delete('1.0', END)
self.txt.insert('1.0', myText)
root.destroy()
def on_closing():
self.txt.tag_delete('highlight')
root.destroy()
findButton = ttk.Button(root, text="Find", command=find)
replaceButton = ttk.Button(root, text="Replace", command=replace)
findButton.grid(row=2, column=0, padx=10, pady=5)
replaceButton.grid(row=2, column=1, padx=10, pady=5)
root.protocol("WM_DELETE_WINDOW", on_closing)
def redraw(self, event=NONE):
self.update_count(event)
self.lineNumber.delete("all")
self.objectIds = []
si = self.txt.index("@0,0")
while True:
dline = self.txt.dlineinfo(si)
if dline is None:
break
y = dline[1]
liNum = str(si).split(".")[0]
self.lineNumber.create_text(
2, y, anchor="nw", text=liNum, fill=self.fontColor)
si = self.txt.index(f"{si}+1line")
def update_count(self, event):
count = self.txt.get('1.0', END)
self.wordCount.set(f"Word Count -> {len(count)-1}")
def font_size(self):
"""Adjust Font Size"""
newFontSize = simpledialog.askstring(
"Font", "Enter font size", parent=self.window)
self.txt.config(font=str(self.fontType + ' ' + newFontSize))
self.txt.update
def speak(self):
engine = pyttsx3.init()
engine.say(self.txt.selection_get())
engine.runAndWait()
def spell_err(self, findString):
"""Check for Spelling Errors"""
startInd = '1.0'
while(startInd):
startInd = self.txt.search(findString, startInd, stopindex=END)
if startInd:
startInd = str(startInd)
lastInd = startInd+f'+{len(findString)}c'
print(startInd, lastInd)
self.txt.tag_add('misspell', startInd, lastInd)
startInd = lastInd
def spell_check(self, event=NONE):
self.txt.tag_delete('misspell')
words = self.txt.get('1.0', "end-1c").split()
for word in words:
# print(word)
if (self.word_exist(word) == FALSE):
self.spell_err(word)
self.txt.tag_config('misspell', background="red", foreground="white")
def word_exist(self, word):
d = enchant.Dict("en_US")
return d.check(word)
def change_theme(self, theme):
if (theme == "blacko"):
self.fontColor = "white"
self.window['theme'] = 'black'
self.txt.config(bg="black", fg="white", insertbackground="white")
self.txt['fg'] = 'white'
self.lineNumber.config(bg="black")
self.menuBar.config(bg="black", fg="white")
pass
elif (theme == "whity"):
self.fontColor = "black"
self.window['theme'] = 'aquativo'
self.lineNumber.config(bg="white")
self.txt.config(bg="white", fg="black", insertbackground="black")
self.menuBar.config(bg="white", fg="black")
pass
elif (theme == "gyona"):
self.fontColor = "black"
self.window['theme'] = 'arc'
self.lineNumber.config(bg="#9de1fd")
self.txt.config(bg="white", fg="black", insertbackground="black")
self.menuBar.config(bg="#9de1fd", fg="black", relief=RAISED)
def about(self):
print("about")
messagebox.showinfo("About", "Your Own Personalized Notepad")
def exit(self):
if(messagebox.askyesno('Quit', 'Are you sure you want to quit')):
self.window.destroy()
a = Editor()