-
Notifications
You must be signed in to change notification settings - Fork 1
/
EditorMul.py
415 lines (291 loc) · 14.9 KB
/
EditorMul.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
import tkinter
import os
from tkinter import *
from tkinter.messagebox import *
from tkinter.filedialog import *
from tkinter import simpledialog
from tkinter import ttk
import threading
import sys
import faulthandler
sys.setrecursionlimit(10**6)
#faulthandler.enable()
class Editor:
__root = Tk()
__thisWidth = 300
__thisHeight = 300
__thisTextArea = Text(__root,bg="white",font="Arial",fg="black",highlightbackground="red",highlightcolor="green",insertbackground="black",selectbackground="cyan",wrap=WORD)
"""Theme changed to light, font black"""
"""Create a text area, menu bar and scroll bar"""
__thisMenuBar = Menu(__root)
__thisFileMenu = Menu(__thisMenuBar, tearoff = 0)
__thisEditMenu = Menu(__thisMenuBar, tearoff = 0)
__thisHelpMenu = Menu(__thisMenuBar, tearoff = 0)
__thisScrollBar = Scrollbar(__thisTextArea)
counter = 0
__file = None
def __init__(self,**kwargs):
try:
self.__root.wm_iconbitmap("Notepad.ico")
except:
pass
try:
self.__thisWidth = kwargs['width']
except KeyError:
pass
try:
self.__thisHeight = kwargs['height']
except KeyError:
pass
self.__root.title("Untitled - Editor")
"""Do not uncomment this part"""
"""Removing bindings also removes binding with keyboard and mouse i.e. user cannot interact with the text area"""
# bindtags = list(self.__thisTextArea.bindtags())
# bindtags.remove("Text")
# self.__thisTextArea.bindtags(tuple(bindtags))
"""If the height is not specified in the keyword arguments given, then use default values specified"""
screenWidth = self.__root.winfo_screenwidth()
screenHeight = self.__root.winfo_screenheight()
left = (screenWidth / 2) - (self.__thisWidth / 2)
top = (screenHeight / 2) - (self.__thisHeight /2)
self.__root.geometry('%dx%d+%d+%d' % (self.__thisWidth,self.__thisHeight,left,top))
self.__root.grid_rowconfigure(0,weight = 1)
self.__root.grid_columnconfigure(0,weight = 1)
self.__thisTextArea.grid(sticky = N + E + S + W)
self.__thisFileMenu.add_command(label = "New",command = self.__newFile, accelerator = "Ctrl + N")
self.__thisFileMenu.add_command(label = "Open",command = self.__openFile, accelerator = "Ctrl + O")
self.__thisFileMenu.add_command(label = "Save",command = self.__saveFile, accelerator = "Ctrl + S")
self.__thisFileMenu.add_command(label = "Save As",command = self.__saveFileas, accelerator = "Ctrl + Shift + V")
self.__thisFileMenu.add_command(label = "Switch Theme",command = self.__theme)
self.__thisFileMenu.add_separator()
self.__thisFileMenu.add_command(label = "Exit",command = self.__quitApplication, accelerator = "Ctrl + Q")
self.__thisMenuBar.add_cascade(label = "File",menu = self.__thisFileMenu)
self.__thisEditMenu.add_command(label = "Select All",command = self.__selectALL, accelerator = "Ctrl + A")
self.__thisEditMenu.add_command(label = "Search",command = self.__find, accelerator = "Ctrl + F")
self.__thisEditMenu.add_command(label = "Search and Replace",command = self.__findNReplace, accelerator = "Ctrl + H")
self.__thisEditMenu.add_command(label = "Cut", command = self.__cut, accelerator = "Ctrl + X")
self.__thisEditMenu.add_command(label = "Copy", command = self.__copy, accelerator = "Ctrl + C")
self.__thisEditMenu.add_command(label = "Paste", command = self.__paste, accelerator = "Ctrl + V")
self.__thisEditMenu.add_command(label = "Clear", command = self.__clear, accelerator = "Ctrl + D")
self.__thisEditMenu.add_command(label = "Spell check", command = self.__SpellCheck, accelerator = "Ctrl + J")
self.__thisMenuBar.add_cascade(label = "Edit",menu = self.__thisEditMenu)
self.__thisHelpMenu.add_command(label = "About",command = self.__showAbout)
self.__thisHelpMenu.add_command(label = "Team",command = self.__showTeam)
self.__thisHelpMenu.add_command(label = "Shortcuts",command = self.__showShortcuts)
self.__thisMenuBar.add_cascade(label = "Help",menu = self.__thisHelpMenu)
self.__root.config(menu = self.__thisMenuBar)
self.__thisScrollBar.pack(side = RIGHT,fill = Y)
self.__thisScrollBar.config(command = self.__thisTextArea.yview)
self.__thisTextArea.config(yscrollcommand = self.__thisScrollBar.set)
"""
Remove all existing keybinds that exist by default in Tkinter to avoid overlap of functions
"""
"""Removing all the shortcuts that may exist with the alphabet keys so that overlap of functions is removed"""
self.__thisTextArea.bind('<Control-A>',lambda event:"break")
self.__thisTextArea.bind('<Control-a>',lambda event:"break")
self.__thisTextArea.bind('<Control-B>',lambda event:"break")
self.__thisTextArea.bind('<Control-b>',lambda event:"break")
self.__thisTextArea.bind('<Control-C>',lambda event:"break")
self.__thisTextArea.bind('<Control-c>',lambda event:"break")
self.__thisTextArea.bind('<Control-D>',lambda event:"break")
self.__thisTextArea.bind('<Control-d>',lambda event:"break")
self.__thisTextArea.bind('<Control-E>',lambda event:"break")
self.__thisTextArea.bind('<Control-e>',lambda event:"break")
self.__thisTextArea.bind('<Control-F>',lambda event:"break")
self.__thisTextArea.bind('<Control-f>',lambda event:"break")
self.__thisTextArea.bind('<Control-G>',lambda event:"break")
self.__thisTextArea.bind('<Control-g>',lambda event:"break")
self.__thisTextArea.bind('<Control-H>',lambda event:"break")
self.__thisTextArea.bind('<Control-h>',lambda event:"break")
self.__thisTextArea.bind('<Control-I>',lambda event:"break")
self.__thisTextArea.bind('<Control-i>',lambda event:"break")
self.__thisTextArea.bind('<Control-J>',lambda event:"break")
self.__thisTextArea.bind('<Control-j>',lambda event:"break")
self.__thisTextArea.bind('<Control-K>',lambda event:"break")
self.__thisTextArea.bind('<Control-k>',lambda event:"break")
self.__thisTextArea.bind('<Control-L>',lambda event:"break")
self.__thisTextArea.bind('<Control-l>',lambda event:"break")
self.__thisTextArea.bind('<Control-M>',lambda event:"break")
self.__thisTextArea.bind('<Control-m>',lambda event:"break")
self.__thisTextArea.bind('<Control-N>',lambda event:"break")
self.__thisTextArea.bind('<Control-n>',lambda event:"break")
self.__thisTextArea.bind('<Control-O>',lambda event:"break")
self.__thisTextArea.bind('<Control-o>',lambda event:"break")
self.__thisTextArea.bind('<Control-P>',lambda event:"break")
self.__thisTextArea.bind('<Control-p>',lambda event:"break")
self.__thisTextArea.bind('<Control-Q>',lambda event:"break")
self.__thisTextArea.bind('<Control-q>',lambda event:"break")
self.__thisTextArea.bind('<Control-R>',lambda event:"break")
self.__thisTextArea.bind('<Control-r>',lambda event:"break")
self.__thisTextArea.bind('<Control-S>',lambda event:"break")
self.__thisTextArea.bind('<Control-s>',lambda event:"break")
self.__thisTextArea.bind('<Control-T>',lambda event:"break")
self.__thisTextArea.bind('<Control-t>',lambda event:"break")
self.__thisTextArea.bind('<Control-U>',lambda event:"break")
self.__thisTextArea.bind('<Control-u>',lambda event:"break")
# self.__thisTextArea.bind('<Control-V>',lambda event:"break")
# self.__thisTextArea.bind('<Control-v>',lambda event:"break")
self.__thisTextArea.bind('<Control-W>',lambda event:"break")
self.__thisTextArea.bind('<Control-w>',lambda event:"break")
self.__thisTextArea.bind('<Control-X>',lambda event:"break")
self.__thisTextArea.bind('<Control-x>',lambda event:"break")
self.__thisTextArea.bind('<Control-Y>',lambda event:"break")
self.__thisTextArea.bind('<Control-y>',lambda event:"break")
self.__thisTextArea.bind('<Control-Z>',lambda event:"break")
self.__thisTextArea.bind('<Control-z>',lambda event:"break")
self.__thisTextArea.bind('<Control-slash>',lambda event:"break")
"""
Bind the shortcuts to the their respective functions
"""
self.__thisTextArea.bind('<Control-N>',self.__newFile)
self.__thisTextArea.bind('<Control-n>',self.__newFile)
self.__thisTextArea.bind('<Control-S>',self.__saveFile)
self.__thisTextArea.bind('<Control-s>',self.__saveFile)
self.__thisTextArea.bind('<Control-Shift-S>',self.__saveFileas)
self.__thisTextArea.bind('<Control-Shift-s>',self.__saveFileas)
self.__thisTextArea.bind('<Control-O>',self.__openFile)
self.__thisTextArea.bind('<Control-o>',self.__openFile)
self.__thisTextArea.bind('<Control-Q>',self.__quitApplication)
self.__thisTextArea.bind('<Control-q>',self.__quitApplication)
self.__thisTextArea.bind('<Control-A>',self.__selectALL)
self.__thisTextArea.bind('<Control-a>',self.__selectALL)
self.__thisTextArea.bind('<Control-F>',self.__find)
self.__thisTextArea.bind('<Control-f>',self.__find)
self.__thisTextArea.bind('<Control-H>',self.__findNReplace)
self.__thisTextArea.bind('<Control-h>',self.__findNReplace)
self.__thisTextArea.bind('<Control-D>',self.__clear)
self.__thisTextArea.bind('<Control-d>',self.__clear)
self.__thisTextArea.bind('<Control-X>',self.__cut)
self.__thisTextArea.bind('<Control-x>',self.__cut)
self.__thisTextArea.bind('<Control-C>',self.__copy)
self.__thisTextArea.bind('<Control-c>',self.__copy)
self.__thisTextArea.bind('<Control-J>',self.__SpellCheck)
self.__thisTextArea.bind('<Control-j>',self.__SpellCheck)
self.__words = open("/usr/share/dict/words").read().split("\n")
# self.__thisTextArea.bind('<Control-V>',self.__paste)
# self.__thisTextArea.bind('<Control-v>',self.__paste)
def __quitApplication(self,event = None):
if messagebox.askokcancel("Quit","Do you wish to exit the Editor?"):
self.__root.destroy()
def __showAbout(self):
showinfo("Editor","Made by Team 2")
def __showTeam(self):
showinfo("Team Members","COE18B018 - G V Anurag\nCOE18B029 - Katte Prahlad Gowtham\nCOE18B056 - Thigulla Vamsi Krishna\nCOE18B065 - Srinivasan R Sharma\nCED18I039 - Paleti Krishnasai\nCED18I056 - Darshan VSS\n")
def __showShortcuts(self):
showinfo("Editor Shortcuts","New File - Ctrl + N\nOpen File - Ctrl + O\nSave File - Ctrl + S\nSave As - Ctrl + Shift + S\nExit - Ctrl + Q\nCut - Ctrl + X\nCopy - Ctrl + C\nPaste - Ctrl + P\n\nFind - Ctrl + F\nFind and Replace - Ctrl + H\nSelect All - Ctrl + A\nSelect - Shift + <Arrow Key>\nBeginning of line - Home\nEnd of line - End\nBeginning of File - Ctrl + Home\nEnd of File - Ctrl + End\n Next Paragraph - Ctrl + <Down>\nPrevious Paragraph - Ctrl + <Up>\nToggle between , . ; - Ctrl + <Left/Right>\n")
def __openFile(self,event = None):
self.__file = askopenfilename(defaultextension =".txt",filetypes=[("All Files","*.*"),("Text Documents","*.txt")])
if self.__file == "":
self.__file = None
else:
self.__root.title(os.path.basename(self.__file)+" - Editor")
self.__thisTextArea.delete(1.0,END)
file = open(self.__file,"r")
self.__thisTextArea.insert(1.0,file.read())
file.close()
def __newFile(self, event = None):
self.__root.title("Untitled - Editor")
self.__file = None
self.__thisTextArea.delete(1.0,END)
"""Clears the text area window"""
def __saveFile(self,event = None):
if self.__file == None:
self.__file = asksaveasfilename(initialfile = 'Untitled.txt',defaultextension = ".txt",filetypes = [("All Files","*.*"),("Text Documents","*.txt")])
if self.__file == "":
self.__file = None
else:
file = open(self.__file,"w")
file.write(self.__thisTextArea.get(1.0,END))
file.close()
self.__root.title(os.path.basename(self.__file)+ " - Editor")
"""This line changes the title of the window"""
else:
file = open(self.__file,"w")
file.write(self.__thisTextArea.get(1.0,END))
file.close()
def __saveFileas(self, event = None):
self.__file = asksaveasfilename(initialfile = 'Untitled.txt',defaultextension = ".txt",filetypes = [("All Files","*.*"),("Text Documents","*.txt")])
if self.__file != "":
file = open(self.__file,"w")
file.write(self.__thisTextArea.get(1.0,END))
file.close()
self.__root.title(os.path.basename(self.__file)+ " - Editor")
"""This line changes the title of the window"""
def __clear(self, event = None):
self.__thisTextArea.delete(1.0,END)
def __cut(self, event = None):
self.__thisTextArea.event_generate("<<Cut>>")
def __copy(self, event = None):
self.__thisTextArea.event_generate("<<Copy>>")
def __paste(self, event = None):
self.__thisTextArea.event_generate("<<Paste>>")
def __selectALL(self,event=None):
self.__thisTextArea.tag_add('sel','1.0',END)
return "break"
"""Add multithreading for find and findNReplace"""
def __finder(self,event=None):
self.__thisTextArea.tag_remove('found','1.0',END)
s = simpledialog.askstring("Find","Enter the string to be found")
if(s):
index = '1.0'
while 1:
index = self.__thisTextArea.search(s,index,nocase = 1,stopindex = END)
if not index:
break
lastindex = '%s+%dc'%(index,len(s))
self.__thisTextArea.tag_add('found',index,lastindex)
index = lastindex
self.__thisTextArea.tag_config('found',foreground="red")
def __find(self,event=None):
t1=threading.Thread(target=self.__finder());
t1.start()
t1.join()
def __findNReplaceF(self,event=None):
self.__thisTextArea.tag_remove('found','1.0',END)
s = simpledialog.askstring("Find","Enter the string to be replaced")
r = simpledialog.askstring("Replace","Enter the string to be replaced with")
if(s and r):
index = '1.0'
while 1:
index = self.__thisTextArea.search(s, index, nocase = 1,stopindex = END)
#print(index)
if not index:
break
lastindex = '%s+%dc'%(index,len(s))
self.__thisTextArea.delete(index,lastindex)
self.__thisTextArea.insert(index,r)
lastindex = '%s+%dc'%(index,len(r))
self.__thisTextArea.tag_add('found',index,lastindex)
index = lastindex
if self.counter == 1:
self.__thisTextArea.tag_config('found',foreground="yellow")
else:
self.__thisTextArea.tag_config('found',foreground="blue")
def __findNReplace(self,event=None):
t1=threading.Thread(target=self.__findNReplaceF())
t1.start()
t1.join()
def __SpellChecker(self,event = None):
self.__file = askopenfilename(defaultextension =".txt",filetypes=[("Text Documents","*.txt")])
cmd = 'aspell -c '+self.__file
os.system(cmd)
self.__thisTextArea.delete(1.0,END)
file = open(self.__file,"r")
self.__thisTextArea.insert(1.0,file.read())
file.close()
return "break"
def __SpellCheck(self,event = None):
t1=threading.Thread(target=self.__SpellChecker())
t1.start()
t1.join()
def __theme(self):
if self.counter!=0:
self.__thisTextArea.config(bg="white",font="Arial",fg="black",highlightbackground="red",highlightcolor="green",insertbackground="black",selectbackground="cyan",wrap=WORD)
self.counter = 0
else:
self.__thisTextArea.config(bg="black",font="Arial",fg="white",highlightbackground="red",highlightcolor="green",insertbackground="white",selectbackground="yellow",wrap=WORD)
self.counter = 1
def run(self):
self.__root.mainloop()
Editor1 = Editor(width=600,height=400)
Editor1.run()