/
GUI.py
434 lines (333 loc) · 16.4 KB
/
GUI.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
from tkinter import *
from tkinter.scrolledtext import ScrolledText
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
import datetime
# pip install tkfontchooser
from tkfontchooser import askfont
class Menubar():
def __init__(self, parent):
# important stuffs ...
font_style = ('arial',10)
menubar = Menu(parent.root,font=font_style)
parent.root.config(menu=menubar)
# file menu -------------------------------------------
file_menu = Menu(menubar,font=font_style,tearoff = 0)
file_menu.add_command(label = "New File",command=parent.new_file , accelerator='Ctrl+N')
file_menu.add_command(label = "Open File" , command = parent.open_file, accelerator='Ctrl+O')
file_menu.add_command(label = "Save " , command = parent.save_file, accelerator='Ctrl+S')
file_menu.add_command(label = "Save As" , command =parent.save_as_file, accelerator='Ctrl+Shift+S')
file_menu.add_separator()
file_menu.add_command(label = "Exit App." , command = parent.close_App, accelerator='Ctrl+Q')
# tool menu -----------------------------
tool_menu = Menu(menubar, font=font_style, tearoff=0)
tool_menu.add_command(label="Find ", command=parent.find_word_window)
tool_menu.add_command(label="Find and Replace ", command=parent.find_replace_window)
tool_menu.add_command(label="Refresh ", command= parent.refresh)
tool_menu.add_command(label="Font ", command=parent.select_font)
tool_menu.add_command(label="Add Time ", command=parent.add_time)
tool_menu.add_command(label="Add Date ", command=parent.add_date)
tool_menu.add_command(label="Add Time/Date ", command=parent.add_time_date)
# About menu -----------------------------
about_menu = Menu(menubar, font=font_style, tearoff=0)
about_menu.add_command(label="About PyNote", command=parent.about_us)
about_menu.add_command(label=" Version ", command=parent.about_pynote)
# configure menubars ....
menubar.add_cascade(label = 'File', menu = file_menu)
menubar.add_cascade(label = 'Tools' , menu = tool_menu)
menubar.add_cascade(label='About', menu=about_menu)
class PyNote:
def __init__(self,root):
self.root = root
self.root.title('Untitled - PyNote')
self.root.configure( bg='lightgray')
self.root.geometry('950x600')
self.root.protocol("WM_DELETE_WINDOW", self.callback)
self.root.minsize(width=700, height=400)
# important stuffs ...
self.font_style = ('arial',14)
self.filename = None
self.file_saved = True
self.index=0
# widgets ,........
self.textarea = ScrolledText(self.root,font=self.font_style,undo=True, wrap = WORD)
#self.textarea.bindtags(('Text','post-class-bindings', '.', 'all'))
self.textarea.pack(fill=BOTH,expand=True)
self.shortcut_binding()
self.status = StringVar()
self.pos = StringVar()
self.status.set('PyNote - ({})'.format('Untitled File'))
# important stuffs ...
font_style = ('arial', 13)
self.label = Label(self.root, textvariable=self.status, fg='black', bg='lightgray', anchor=SW, font=font_style)
self.label.pack(side=LEFT,fill=BOTH)
# cursor postion label...
self.cursor_pos_lbl = Label(self.root, textvariable=self.pos, fg='black', bg='lightgray', anchor=NW, font=font_style)
self.cursor_pos_lbl.pack(side=RIGHT)
# code for popup menu bar ....
# creating menu-bar ...
self.m = Menu(root, tearoff=0)
self.m.add_command(label="Refresh", command=self.refresh)
self.m.add_command(label = "Select All",command=self.select_all)
self.m.add_command(label="Cut", command=lambda: self.textarea.event_generate("<<Cut>>"))
self.m.add_command(label="Copy", command=lambda: self.textarea.event_generate("<<Copy>>"))
self.m.add_command(label = "Paste", command=lambda: self.textarea.event_generate("<<Paste>>"))
self.m.add_command(label="Delete", command=lambda: self.textarea.delete(SEL_FIRST, SEL_LAST))
self.m.add_command(label="Add Date/Time", command=self.add_time_date)
# Calling functions and other class methods or creating objects ......
self.menubar = Menubar(self)
# function definations .................
def set_title(self,name= None):
if name:
self.root.title(name + " - PyNote")
else:
self.root.title('Untitled - PyNote')
def new_file(self, event = None):
self.textarea.delete(1.0, END)
# call the save command if any text is written on text area ..
self.filename = None
self.set_title()
self.update_status('PyNote - ({})'.format('Untitled File'))
def open_file(self, *args):
self.filename = filedialog.askopenfilename( defaultextension = ".txt",
filetypes = [("All Files" , "*.*"),
("Text Files" , "*.txt"),
("Python Scripts" , "*.py"),
('HTML Docs.' , "*.html"),
("CSS Docs.","*.css")])
if self.filename:
self.textarea.delete(1.0,END)
with open(self.filename , "r") as f :
self.textarea.insert(1.0,f.read())
self.update_status('File is Opened / ' + self.filename)
self.set_title(self.filename)
def save_file(self, *args):
if self.filename:
try:
textarea_data = self.textarea.get(1.0,END)
with open(self.filename, 'w') as f:
f.write(textarea_data)
self.update_status('File is Saved Again ')
self.file_saved = True
except Exception as e:
messagebox.showerror('PyNote -Says ','Error Occurs '+ str(e))
else:
self.save_as_file()
def save_as_file(self, *args):
try:
new_file = filedialog.asksaveasfilename(initialfile = 'Untitled.txt',
defaultextension=".txt",
filetypes=[("All Files", "*.*"),
("Text Files", "*.txt"),
("Python Scripts", "*.py"),
('HTML Docs.', "*.html"),
("CSS Docs.", "*.css")]
)
textarea_data = self.textarea.get(1.0,END)
with open(new_file , 'w') as f:
f.write(textarea_data)
self.filename = new_file
self.set_title(self.filename)
self.file_saved = True
self.update_status('File is Saved As '+ self.filename)
except Exception as e:
messagebox.showerror('PyNote -Says ','Error Occurs '+ str(e))
def about_us(self):
messagebox.showinfo('About - Us','''
PyNote is a Text Editor which is looks like a Notepad \n
PyNote is Build as a Demo Project for those Who are \n
interested in building Awesome GUI Projects using Tkinter\n
_________________________________________________________\n
PyNote Developer : Yogesh Singh \n
Build For : Dynamic Coding \n''')
def about_pynote(self):
messagebox.showinfo('About - PyNote','''
Current Version : 0.0 \n
''')
def shortcut_binding(self):
self.textarea.bind('<Control-n>',self.new_file)
self.textarea.bind('<Control-Key-o>',self.open_file)
self.textarea.bind('<Control-s>',self.save_file)
self.textarea.bind('<Control-S>',self.save_as_file)
self.textarea.bind('<Control-q>',self.close_App)
self.textarea.bind('<Key>', self.text_area_cursor)
#self.textarea.bind('<KeyPress>', self.cursor_pos)
self.root.bind("<Button-3>", self.do_popup)
def do_popup(self, event):
try:
self.m.tk_popup(event.x_root, event.y_root)
finally:
self.m.grab_release()
def find_word_window(self):
top = Toplevel(self.root)
top.geometry('400x130')
top.title('PyNote - Find Text ')
top.resizable(0,0)
# important stuffs ...........
find_var = StringVar()
self.total_var =StringVar()
# --------------------------find window widgets ----------------------
self.find_entry = ttk.Entry(top,width=20,font=('times',12), textvariable = find_var)
self.find_entry.focus_set()
self.find_entry.bind('<Return>',self.find)
self.find_entry.place(x=10,y=25)
find_btn = Button(top,text='Find',width=10,bd=2,relief=RIDGE,command=self.find)
find_btn.place(x=200,y=25)
clear_btn = Button(top, text='Clear', width=10, bd=2, relief=RIDGE,command=lambda:find_var.set(''))
clear_btn.place(x=300, y=25)
self.total_world_count = Label(top,font=('arial',13,'bold'))
self.total_world_count.place(x=20,y=80)
top.mainloop()
def find(self, *args):
# remove tag 'found' from index 1 to END
self.textarea.tag_remove('found', '1.0', END)
# returns to widget currently in focus
s = self.find_entry.get()
if (s):
idx = '1.0'
self.count=0
while 1:
# searches for desried string from index 1
idx = self.textarea.search(s, idx, nocase=1,
stopindex=END)
self.count +=1
if not idx: break
# last index sum of current index and
# length of text
lastidx = '% s+% dc' % (idx, len(s))
# overwrite 'Found' at idx
self.textarea.tag_add('found', idx, lastidx)
idx = lastidx
# mark located string as red
self.textarea.tag_config('found', foreground='green', background='yellow')
#self.textarea.focus_set()
self.total_world_count['text'] = 'Total Word Count is : ' + str(self.count -1 )
def refresh(self):
self.textarea.tag_delete("found")
def find_replace_window(self):
top = Toplevel(self.root)
top.geometry('400x130')
top.title('PyNote - Find Text ')
top.focus_force()
top.resizable(0, 0)
# important stuffs ...........
find_var = StringVar()
replace_var = StringVar()
# --------------------------find replace window widgets ----------------------
self.find_entry = ttk.Entry(top, width=20, font=('times', 12), textvariable=find_var)
self.find_entry.focus_set()
self.find_entry.bind('<Return>', self.find)
self.find_entry.place(x=10, y=25)
self.replace_entry = ttk.Entry(top, width=20, font=('times', 12), textvariable=replace_var)
#self.replace_entry.focus_set()
self.replace_entry.bind('<Return>', self.find_replace)
self.replace_entry.place(x=10, y=60)
find_btn = Button(top, text='Find', width=10, bd=2, relief=RIDGE, command=self.find)
find_btn.place(x=200, y=25)
replace_btn = Button(top, text='Replace', width=10, bd=2, relief=RIDGE,
command=self.find_replace)
replace_btn.place(x=200, y=60)
clear_btn = Button(top, text='Clear All', width=10,height=3, bd=2, relief=RIDGE, command=lambda: [find_var.set(''),replace_var.set('')])
clear_btn.place(x=300, y=26)
self.total_world_count = Label(top, text='',font=('arial', 13, 'bold'))
self.total_world_count.place(x=20, y=100)
top.mainloop()
def find_replace(self, *args):
find = self.find_entry.get()
replace = self.replace_entry.get()
if(find and replace):
idx = '1.0'
while 1:
# searches for desired string from index 1
idx = self.textarea.search(find, idx, nocase=1,
stopindex=END)
print(idx)
if not idx: break
# last index sum of current index and
# length of text
lastidx = '% s+% dc' % (idx, len(find))
self.textarea.delete(idx, lastidx)
self.textarea.insert(idx, replace)
lastidx = '% s+% dc' % (idx, len(replace))
# overwrite 'Found' at idx
self.textarea.tag_add('found', idx, lastidx)
idx = lastidx
self.total_world_count['text'] = '" {} " is replaced with " {} "'.format(find,replace)
self.replace_entry.focus_set()
def close_App(self, *args):
print(self.textarea.get(1.0,END))
if self.textarea.get(1.0,END) != '' and self.file_saved == True:
if messagebox.askyesno('PyNote Says','Do you really want to exit'):
self.root.quit()
else:
val = messagebox.askyesnocancel('PyNote - Says', 'Save File Before Exit App. ')
if val:
self.save_file()
self.root.destroy()
elif val == False:
self.root.destroy()
def text_area_cursor(self, *args):
#print('inside function ')
self.file_saved = False
if self.filename:
self.update_status('PyNote - Currently Working on : ({})'.format(self.filename))
else:
self.update_status('Untitled File')
pos = self.textarea.index(INSERT)
line , column = pos.split('.')
column = int(column) + 1
#print('Current line is : ',line)
#print('Current Column is : ', column)
self.pos.set('Line : {} Column : {}'.format(line,column))
def cursor_pos(self, *args):
pos = self.textarea.index(INSERT)
line, column = pos.split('.')
column = int(column) + 1
self.pos.set('Line : {} Column : {}'.format(line, column))
def update_status(self,data=''):
self.status.set('PyNote - ({})'.format(data))
def select_font(self):
font= askfont()
# font is "" if the user has cancelled
if font:
# spaces in the family name need to be escaped
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.font_style = font_str
self.textarea.configure(font=self.font_style)
def add_time(self):
now = datetime.datetime.now()
now = now.strftime(' Time: %I:%M:%S:%p ')
self.textarea.insert(END, now)
def add_date(self):
now = datetime.datetime.now()
now = now.strftime(' %Y-%m-%d ')
self.textarea.insert(END, now)
def add_time_date(self):
now = datetime.datetime.now()
now = now.strftime(' Date: %Y-%m-%d Time: %I:%M:%S:%p ')
self.textarea.insert(END,now)
# Select all the text in textbox
def select_all(self, *args):
self.textarea.tag_add(SEL, "1.0", END)
self.textarea.mark_set(INSERT, "1.0")
self.textarea.see(INSERT)
def callback(self):
if(self.file_saved) and (self.textarea.get(1.0,END)) != '':
if messagebox.askokcancel("Quit", "Do you really wish to quit?"):
self.root.destroy()
else:
val = messagebox.askyesnocancel('PyNote - Says','Save File Before Exit App. ')
if val:
self.save_file()
elif val== False:
self.root.destroy()
if __name__ == '__main__':
root= Tk()
py = PyNote(root)
root.mainloop()