-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotepy.py
513 lines (393 loc) · 18.9 KB
/
notepy.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
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog as fd
from tkinter import ttk
import os
import webbrowser
#--- making the user interface using tkinter ---
#--- using the Notepy class we create an app object which runs when the program starts ---
#--- the Notepy class is an extensions of Tk class ---
class Notepy(Tk):
def __init__(self):
#--- the selected widget ---
self.selectedWidget = None
#--- the opened file name ---
self.filename = None
self.checkOpenF()
#--- the self.window ---
self.win = Tk()
self.win.geometry("640x480")
self.win.resizable(True, True)
#--- the menu self.bar ---
self.bar = Menu(self.win)
#--- the file menu ---
self.fmenu = Menu(self.bar, tearoff = 0)
self.fmenu.add_command(label="{:<16}".format("New"), accelerator="Ctrl+N",command=self.makeNew)
self.fmenu.add_command(label="{:<16}".format("Open"), accelerator="Ctrl+O", command=self.openFile)
self.fmenu.add_command(label="{:<16}".format("Save"), accelerator="Ctrl+S", command=self.saveFile)
self.fmenu.add_command(label="{:<16}".format("Save as"), accelerator="Alt+S", command=self.saveAs)
self.fmenu.add_separator()
self.fmenu.add_command(label="{:<16}".format("Exit"), accelerator="Alt + F4", command=self.win.destroy)
#--- the edit menu ---
self.emenu = Menu(self.bar, tearoff = 0)
self.emenu.add_command(label="{:<16}".format("Cut"), accelerator="Ctrl + X", command=self.cutText)
self.emenu.add_command(label="{:<16}".format("Copy"), accelerator="Ctrl + C", command=self.copyText)
self.emenu.add_command(label="{:<16}".format("Paste"),accelerator="Ctrl + V", command=self.pasteText)
self.emenu.add_separator()
self.emenu.add_command(label="{:<16}".format("Select All"),accelerator="Ctrl + A", command=self.selectAllText)
#--- the help menu ---
self.hmenu = Menu(self.bar, tearoff = 0)
self.hmenu.add_command(label="{:<16}".format("Documentation"), accelerator="F11", command=self.showDoc)
self.hmenu.add_separator()
self.hmenu.add_command(label="{:<16}".format("Credits"), accelerator="F12", command=self.showCredits)
self.hmenu.bind("F11", self.showDoc)
self.hmenu.bind("F12", self.showCredits)
#--- theme menu ---
self.tmenu = Menu(self.bar, tearoff=0)
#--- theme selection ---
self.tmenu.add_command(label="{:<16}".format("Switch Light/Dark Theme"), accelerator="F6", command=self.changeTheme)
#--- right click menu ---
self.rclkmenu = Menu(self.win, tearoff=0)
#--- string formatting to make the text in right context menu have a distance ---
self.rclkmenu.add_command(label="{:<16}".format("Cut"), accelerator="Ctrl+X", command=self.cutText)
self.rclkmenu.add_command(label="{:<16}".format("Copy"), accelerator="Ctrl+C", command=self.copyText)
self.rclkmenu.add_command(label="{:<16}".format("Paste"), accelerator="Ctrl+V", command=self.pasteText)
self.rclkmenu.add_separator()
self.rclkmenu.add_command(label="{:<16} {:>32}".format("Select All","Ctrl + A"), underline=4, accelerator="Ctrl+A", command=self.selectAllText)
self.bar.add_cascade(label="File", menu=self.fmenu)
self.bar.add_cascade(label="Edit", menu=self.emenu)
self.bar.add_cascade(label="Help", menu=self.hmenu)
self.bar.add_cascade(label="Options", menu=self.tmenu)
#--- vertical and horizontal scrollbars ---
self.vscrollbar = Scrollbar(self.win, orient="vertical")
self.vscrollbar.pack(side=RIGHT, fill=Y)
self.hscrollbar = Scrollbar(self.win, orient="horizontal")
self.hscrollbar.pack(side=BOTTOM, fill=X)
#--- making the text ---
self.txt = Text(self.win, bg="white", fg="black", wrap=WORD, width=640, height=480, insertbackground="black", font = ("Sans-Serif", "16"), yscrollcommand = self.vscrollbar.set, xscrollcommand = self.hscrollbar.set)
self.txt.bind("<Button-3>", self.showRightClickMenu)
self.txt.bind("<Control-A>", self.selectAllText)
self.txt.pack()
#--- configuring the scrollbars ---
self.vscrollbar.config(command=self.txt.yview)
self.hscrollbar.config(command=self.txt.xview)
self.win.title("{:<16}".format("Notepy - The Python-made Notepad by nikosnikitas"))
self.win.config(menu=self.bar)
self.win.mainloop()
#--- on right click of the mouse we show a menu to the user ---
def showRightClickMenu(self, event):
self.rclkmenu.post(event.x_root, event.y_root)
self.selectedWidget = event.widget
#--- cuts text to clipboard ---
def cutText(self, event=None):
try:
self.txt.selection_get()
self.txt.event_generate("<<Cut>>")
except:
messagebox.showwarning("Warning","Please select the text first.")
#--- copies the selected text to clipboard ---
def copyText(self, event=None):
try:
self.txt.selection_get()
self.txt.event_generate("<<Copy>>")
except:
messagebox.showwarning("Warning","Please select the text first.")
#--- pastes text from clipboard to the editor ---
def pasteText(self, event=None):
self.txt.event_generate("<<Paste>>")
#--- selects all text in the editor ---
def selectAllText(self, event=None):
#--- adding a tag to make a selected text ---
self.txt.tag_add('sel', '1.0', 'end')
return "break"
#--- shows documentation ---
def showDoc(self):
docwin = Tk()
docwin.title("Notepy Documentation")
docwin.geometry("640x480")
lbl = Label(docwin, text="Notepy - The Python-made Notepad")
lbl.pack()
details = Label(docwin, text="Made with ♥ in Python 3\n You may use this as your notepad.\nWith basic functionalities like Cut, Copy, Paste.\nYou can create and edit text files with ease.\n HOW TO USE\n File - Here you can:\n 1. NEW - open a new Notepy\n OPEN a new file\n SAVE the current file\n SAVE AS a file with a different name\n EXIT the application.\n Edit - here you can: CUT text (after selecting it)\nCOPY text (after selecting it)\n PASTE text from your clipboard.\n SELECT ALL text.\n Help - here you can:\n DOCUMENTATION - read the application's documentation and get help.\n CREDITS - Learn about the application's developer and contact him.\n Options - here you can:\n SWITCH LIGHT/DARK THEME - Change the theme from light to dark and vice versa.")
details.pack()
docwin.mainloop()
#--- open a URL in the browser ---
def openUrl(self, url2open):
webbrowser.open_new(url2open)
#--- shows credits ---
def showCredits(self):
credwin = Tk()
credwin.title("Notepy Credits")
credwin.geometry("420x200")
lbl = Label(credwin, text="Notepy - The Python-made Notepad")
lbl.pack()
ghLinkLbl = Label(credwin, text="You may find the code of this project and more at my GitHub: ")
ghLinkLbl.pack()
ghLink = Label(credwin, text="nikosnikitas", fg="blue", cursor="hand2")
ghLink.pack()
ghLink.bind(
"<Button-1>",
lambda x: self.openUrl("https://github.com/nikosnikitas")
)
ldLinkLbl = Label(credwin, text="Let's connect on Linkedin")
ldLinkLbl.pack()
ldLink = Label(credwin, text="Nikos-Nikitas", fg="blue", cursor="hand2")
ldLink.pack()
ldLink.bind(
"<Button-1>",
lambda x: self.openUrl("https://www.linkedin.com/in/nikos-nikitas-g-0a81931b5")
)
credits = Label(credwin, text="Made with ♥ by Nikos-Nikitas")
credits.pack()
credwin.mainloop()
#--- make a new file ---
def makeNew(self):
os.system("python main.py")
#--- open a file ---
def openFile(self):
self.txt.delete("1.0", END)
ft = [("Text Files", "*.txt"),("Python Files","*.py"), ("All Files","*")]
fn = fd.Open(filetypes=ft)
self.filename = fn
files = fn.show()
if files != "":
contents = self.readF(files)
self.txt.insert(END, contents)
#--- read file ---
def readF(self,f):
flnm = open(f, "r")
fcontent = flnm.read()
self.filename = flnm
return fcontent
#--- save current file --- //ToDo: implement this functionality
def saveFile(self):
try:
self.saveFile = open("New File.self.txt","w")
self.saveFile.write(self.txt.get("1.0", "end"))
self.saveFile.close()
except:
messagebox.shoself.winfo("Hey!","No Open File")
#--- save as a different file ---
def saveAs(self):
fl = fd.askself.saveAsself.filename(defaultextension=".self.txt")
if fl is None:
return
whatToSave = self.txt.get("1.0","end")
with open(fl, "w") as sf:
sf.write(whatToSave)
sf.close()
#--- checks for open file and opens one ---
def checkOpenF(self):
filecount = 0
if self.filename == None:
self.filename = open("New File.txt","w")
return str(self.filename.name)
if self.filename == "New File.txt":
filecount += 1
self.filename = open(f"New File.self.txt{filecount}","w")
return str(self.filename.name)
#--- get theme and change theme ---
def changeTheme(self):
if self.txt["bg"] == "white":
self.txt.config(bg="black", fg="white", insertbackground="white")
self.txt.update()
else:
self.txt.config(bg="white", fg="black", insertbackground="black")
self.txt.update()
if self.bar["bg"] == "white":
self.bar.config(bg="black", fg="white")
self.bar.update()
else:
self.bar.config(bg="white", fg="black")
self.bar.update()
class NotepyGr(Tk):
def __init__(self):
#--- the selected widget ---
self.selectedWidget = None
#--- the opened file name ---
self.filename = None
self.checkOpenF()
#--- the self.window ---
self.win = Tk()
self.win.geometry("640x480")
self.win.resizable(True, True)
#--- the menu self.bar ---
self.bar = Menu(self.win)
#--- the file menu ---
self.fmenu = Menu(self.bar, tearoff = 0)
self.fmenu.add_command(label="{:<16}".format("Νέο"), accelerator="Ctrl+N",command=self.makeNew)
self.fmenu.add_command(label="{:<16}".format("Άνοιγμα"), accelerator="Ctrl+O", command=self.openFile)
self.fmenu.add_command(label="{:<16}".format("Αποθήκευση"), accelerator="Ctrl+S", command=self.saveFile)
self.fmenu.add_command(label="{:<16}".format("Αποθήκευση ως"), accelerator="Alt+S", command=self.saveAs)
self.fmenu.add_separator()
self.fmenu.add_command(label="{:<16}".format("Έξοδος"), accelerator="Alt + F4", command=self.win.destroy)
#--- the edit menu ---
self.emenu = Menu(self.bar, tearoff = 0)
self.emenu.add_command(label="{:<16}".format("Αποκοπή"), accelerator="Ctrl + X", command=self.cutText)
self.emenu.add_command(label="{:<16}".format("Αντιγραφή"), accelerator="Ctrl + C", command=self.copyText)
self.emenu.add_command(label="{:<16}".format("Επικόλληση"),accelerator="Ctrl + V", command=self.pasteText)
self.emenu.add_separator()
self.emenu.add_command(label="{:<16}".format("Επιλογή όλων"),accelerator="Ctrl + A", command=self.selectAllText)
#--- the help menu ---
self.hmenu = Menu(self.bar, tearoff = 0)
self.hmenu.add_command(label="{:<16}".format("Εγχειρίδιο"), accelerator="F11", command=self.showDoc)
self.hmenu.add_separator()
self.hmenu.add_command(label="{:<16}".format("Ευχαριστίες"), accelerator="F12", command=self.showCredits)
self.hmenu.bind("F11", self.showDoc)
self.hmenu.bind("F12", self.showCredits)
#--- theme menu ---
self.tmenu = Menu(self.bar, tearoff=0)
#--- theme selection ---
self.tmenu.add_command(label="{:<16}".format("Αλλαγή Θέματος Φωτεινό/Σκοτεινό"), accelerator="F6", command=self.changeTheme)
#--- right click menu ---
self.rclkmenu = Menu(self.win, tearoff=0)
#--- string formatting to make the text in right context menu have a distance ---
self.rclkmenu.add_command(label="{:<16}".format("Αποκοπή"), accelerator="Ctrl+X", command=self.cutText)
self.rclkmenu.add_command(label="{:<16}".format("Αντιγραφή"), accelerator="Ctrl+C", command=self.copyText)
self.rclkmenu.add_command(label="{:<16}".format("Επικόλληση"), accelerator="Ctrl+V", command=self.pasteText)
self.rclkmenu.add_separator()
self.rclkmenu.add_command(label="{:<16} {:>32}".format("Επιλογή όλων","Ctrl + A"), underline=4, accelerator="Ctrl+A", command=self.selectAllText)
self.bar.add_cascade(label="Αρχείο", menu=self.fmenu)
self.bar.add_cascade(label="Επεξεργασία", menu=self.emenu)
self.bar.add_cascade(label="Βοήθεια", menu=self.hmenu)
self.bar.add_cascade(label="Ρυθμίσεις", menu=self.tmenu)
#--- vertical and horizontal scrollbars ---
self.vscrollbar = Scrollbar(self.win, orient="vertical")
self.vscrollbar.pack(side=RIGHT, fill=Y)
self.hscrollbar = Scrollbar(self.win, orient="horizontal")
self.hscrollbar.pack(side=BOTTOM, fill=X)
#--- making the text ---
self.txt = Text(self.win, bg="white", fg="black", wrap=WORD, width=640, height=480, insertbackground="black", font = ("Sans-Serif", "16"), yscrollcommand = self.vscrollbar.set, xscrollcommand = self.hscrollbar.set)
self.txt.bind("<Button-3>", self.showRightClickMenu)
self.txt.bind("<Control-A>", self.selectAllText)
self.txt.pack()
#--- configuring the scrollbars ---
self.vscrollbar.config(command=self.txt.yview)
self.hscrollbar.config(command=self.txt.xview)
self.win.title("{:<16}".format("Notepy - Το Σημειωματάριο σε Python από τον Νίκο-Νικήτα (GitHub: nikosnikitas)"))
self.win.config(menu=self.bar)
self.win.mainloop()
#--- on right click of the mouse we show a menu to the user ---
def showRightClickMenu(self, event):
self.rclkmenu.post(event.x_root, event.y_root)
self.selectedWidget = event.widget
#--- cuts text to clipboard ---
def cutText(self, event=None):
try:
self.txt.selection_get()
self.txt.event_generate("<<Cut>>")
except:
messagebox.showwarning("Ειδοποίηση","Επιλέξτε το κείμενο πρώτα.")
#--- copies the selected text to clipboard ---
def copyText(self, event=None):
try:
self.txt.selection_get()
self.txt.event_generate("<<Copy>>")
except:
messagebox.showwarning("Ειδοποίηση","Επιλέξτε το κείμενο πρώτα.")
#--- pastes text from clipboard to the editor ---
def pasteText(self, event=None):
self.txt.event_generate("<<Paste>>")
#--- selects all text in the editor ---
def selectAllText(self, event=None):
#--- adding a tag to make a selected text ---
self.txt.tag_add('sel', '1.0', 'end')
return "break"
#--- shows documentation ---
def showDoc(self):
docwin = Tk()
docwin.title("Εγχειρίδιο του Notepy")
docwin.geometry("640x480")
lbl = Label(docwin, text="Notepy - Το Σημειωματάριο σε Python")
lbl.pack()
details = Label(docwin, text="""Φτιαγμένο με ♥ σε Python 3\n Μπορείτε να το χρησιμοποιήσετε ως το σημειωματάριό σας.\nΜε δυο θέματα να επιλέξετε, και βασικές επιλογές επεξεργασίας αρχείων κειμένου και Python.\nΜπορείτε εύκολα να επεξεργαστείτε και να δημιουργήσετε αρχεία.\n Αρχείο \n NEO - Ανοίγει νέο κενό σημειωματάριο.\n ΑΝΟΙΓΜΑ - Ανοίγει ένα αρχείο από τον υπολογιστή.\n ΑΠΟΘΗΚΕΥΣΗ - Αποθηκεύει το τρέχον αρχείο.\n ΑΠΟΘΗΚΕΥΣΗ ΩΣ διαφορετικό αρχείο.\n ΕΞΟΔΟΣ από το πρόγραμμα.\n Επεξεργασία\n ΑΠΟΚΟΠΗ του επιλεγμένου κειμένου.\nΑΝΤΙΓΡΑΦΗ του επιλεγμένου κειμένου.\nΕΠΙΚΟΛΛΗΣΗ κειμένου από το πρόχειρο\n ΕΠΙΛΟΓΗ ΟΛΟΥ του κειμένου\n Βοήθεια\n ΕΓΧΕΙΡΙΔΙΟ - Οδηγίες χρήσης του προγράμματος και γενικές πληροφορίες.\nΕΥΧΑΡΙΣΤΙΕΣ - Σχετικά με τον προγραμματιστή που ανέπτυξε αυτή την εφαρμογή και στοιχεία επικοινωνίας.\n Ρυθμίσεις\n ΑΛΛΑΓΗ ΘΕΜΑΤΟΣ Φωτεινό/Σκοτεινό - Αλλάζει το θέμα του προγράμματος και του κειμένου από φωτεινό σε σκοτεινό και αντίστροφα.""")
details.pack()
docwin.mainloop()
#--- open a URL in the browser ---
def openUrl(self, url2open):
webbrowser.open_new(url2open)
#--- shows credits ---
def showCredits(self):
credwin = Tk()
credwin.title("Ευχαριστίες για το Notepy")
credwin.geometry("360x180")
lbl = Label(credwin, text="Notepy - Το Σημειωματάριο σε Python")
lbl.pack()
ghLinkLbl = Label(credwin, text="Βρείτε τον κώδικα αυτού του προγράμματος και πολλών άλλων στο GitHub μου: ")
ghLinkLbl.pack()
ghLink = Label(credwin, text="nikosnikitas", fg="blue", cursor="hand2")
ghLink.pack()
ghLink.bind(
"<Button-1>",
lambda x: self.openUrl("https://github.com/nikosnikitas")
)
ldLinkLbl = Label(credwin, text="Βρείτε με στο Linkedin")
ldLinkLbl.pack()
ldLink = Label(credwin, text="Nikos-Nikitas", fg="blue", cursor="hand2")
ldLink.pack()
ldLink.bind(
"<Button-1>",
lambda x: self.openUrl("https://www.linkedin.com/in/nikos-nikitas-g-0a81931b5")
)
credits = Label(credwin, text="Φτιαγμένο με ♥ από τον Νίκο-Νικήτα.")
credits.pack()
credwin.mainloop()
#--- make a new file ---
def makeNew(self):
os.system("python main.py")
#--- open a file ---
def openFile(self):
self.txt.delete("1.0", END)
ft = [("Αρχεία Κειμένου", "*self.txt"),("Αρχεία Python","*.py"), ("Όλα τα Αρχεία","*")]
fn = fd.Open(filetypes=ft)
self.filename = fn
files = fn.show()
if files != "":
contents = self.readF(files)
self.txt.insert(END, contents)
#--- read file ---
def readF(self,f):
flnm = open(f, "r")
fcontent = flnm.read()
self.filename = flnm
return fcontent
#--- save current file --- //ToDo: implement this functionality
def saveFile(self):
try:
self.saveFile = open("New File.self.txt","w")
self.saveFile.write(self.txt.get("1.0", "end"))
self.saveFile.close()
except:
messagebox.shoself.winfo("Hey!","No Open File")
#--- save as a different file ---
def saveAs(self):
fl = fd.askself.saveAsself.filename(defaultextension=".self.txt")
if fl is None:
return
whatToSave = self.txt.get("1.0","end")
with open(fl, "w") as sf:
sf.write(whatToSave)
sf.close()
#--- checks for open file and opens one ---
def checkOpenF(self):
filecount = 0
if self.filename == None:
self.filename = open("New File.txt","w")
return str(self.filename.name)
if self.filename == "New File.txt":
filecount += 1
self.filename = open(f"New File.self.txt{filecount}","w")
return str(self.filename.name)
#--- get theme and change theme ---
def changeTheme(self):
if self.txt["bg"] == "white":
self.txt.config(bg="black", fg="white", insertbackground="white")
self.txt.update()
else:
self.txt.config(bg="white", fg="black", insertbackground="black")
self.txt.update()
if self.bar["bg"] == "white":
self.bar.config(bg="black", fg="white")
self.bar.update()
else:
self.bar.config(bg="white", fg="black")
self.bar.update()