-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotepad-using-python.py
More file actions
137 lines (113 loc) · 4.45 KB
/
Notepad-using-python.py
File metadata and controls
137 lines (113 loc) · 4.45 KB
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
#importing required packages and libraries
import re
from tkinter import *
from tkinter.ttk import *
from datetime import datetime
from tkinter import messagebox
from tkinter import filedialog,simpledialog
from tkinter.scrolledtext import ScrolledText
#the root widget
root = Tk()
root.title('GetProjects.org - Notepad using Python')
root.resizable(0, 0)
#creating scrollable notepad window
notepad = ScrolledText(root, width = 90, height = 40)
fileName = ''
#defining functions for commands
def cmdNew(): #file menu New option
global fileName
if len(notepad.get('1.0', END+'-1c'))>0:
if messagebox.askyesno("Notepad", "Do you want to save changes?"):
cmdSave()
else:
notepad.delete(0.0, END)
root.title("Notepad")
def cmdOpen(): #file menu Open option
fd = filedialog.askopenfile(parent = root, mode = 'r')
t = fd.read() #t is the text read through filedialog
notepad.delete(0.0, END)
notepad.insert(0.0, t)
def cmdSave(): #file menu Save option
fd = filedialog.asksaveasfile(mode = 'w', defaultextension = '.txt')
if fd!= None:
data = notepad.get('1.0', END)
try:
fd.write(data)
except:
messagebox.showerror(title="Error", message = "Not able to save file!")
def cmdSaveAs(): #file menu Save As option
fd = filedialog.asksaveasfile(mode='w', defaultextension = '.txt')
t = notepad.get(0.0, END) #t stands for the text gotten from notepad
try:
fd.write(t.rstrip())
except:
messagebox.showerror(title="Error", message = "Not able to save file!")
def cmdExit(): #file menu Exit option
if messagebox.askyesno("Notepad", "Are you sure you want to exit?"):
root.destroy()
def cmdCut(): #edit menu Cut option
notepad.event_generate("<<Cut>>")
def cmdCopy(): #edit menu Copy option
notepad.event_generate("<<Copy>>")
def cmdPaste(): #edit menu Paste option
notepad.event_generate("<<Paste>>")
def cmdClear(): #edit menu Clear option
notepad.event_generate("<<Clear>>")
def cmdFind(): #edit menu Find option
notepad.tag_remove("Found",'1.0', END)
find = simpledialog.askstring("Find", "Find what:")
if find:
idx = '1.0' #idx stands for index
while 1:
idx = notepad.search(find, idx, nocase = 1, stopindex = END)
if not idx:
break
lastidx = '%s+%dc' %(idx, len(find))
notepad.tag_add('Found', idx, lastidx)
idx = lastidx
notepad.tag_config('Found', foreground = 'white', background = 'blue')
notepad.bind("<1>", click)
def click(event): #handling click event
notepad.tag_config('Found',background='white',foreground='black')
def cmdSelectAll(): #edit menu Select All option
notepad.event_generate("<<SelectAll>>")
def cmdTimeDate(): #edit menu Time/Date option
now = datetime.now()
# dd/mm/YY H:M:S
dtString = now.strftime("%d/%m/%Y %H:%M:%S")
label = messagebox.showinfo("Time/Date", dtString)
def cmdAbout(): #help menu About option
label = messagebox.showinfo("About Notepad", "Notepad by - \nGetProjects.org")
#notepad menu items
notepadMenu = Menu(root)
root.configure(menu=notepadMenu)
#file menu
fileMenu = Menu(notepadMenu, tearoff = False)
notepadMenu.add_cascade(label='File', menu = fileMenu)
#adding options in file menu
fileMenu.add_command(label='New', command = cmdNew)
fileMenu.add_command(label='Open...', command = cmdOpen)
fileMenu.add_command(label='Save', command = cmdSave)
fileMenu.add_command(label='Save As...', command = cmdSaveAs)
fileMenu.add_separator()
fileMenu.add_command(label='Exit', command = cmdExit)
#edit menu
editMenu = Menu(notepadMenu, tearoff = False)
notepadMenu.add_cascade(label='Edit', menu = editMenu)
#adding options in edit menu
editMenu.add_command(label='Cut', command = cmdCut)
editMenu.add_command(label='Copy', command = cmdCopy)
editMenu.add_command(label='Paste', command = cmdPaste)
editMenu.add_command(label='Delete', command = cmdClear)
editMenu.add_separator()
editMenu.add_command(label='Find...', command = cmdFind)
editMenu.add_separator()
editMenu.add_command(label='Select All', command = cmdSelectAll)
editMenu.add_command(label='Time/Date', command = cmdTimeDate)
#help menu
helpMenu = Menu(notepadMenu, tearoff = False)
notepadMenu.add_cascade(label='Help', menu = helpMenu)
#adding options in help menu
helpMenu.add_command(label='About Notepad', command = cmdAbout)
notepad.pack()
root.mainloop()