-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
193 lines (155 loc) · 6.36 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
# Importing Required libraries & Modules
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
root = Tk()
class TextEditor:
def __init__(self,root):
self.root = root
self.root.title("TEXT EDITOR")
self.root.geometry("1200x700+200+150")
self.filename = None
self.title = StringVar()
self.status = StringVar()
self.titlebar = Label(self.root,textvariable=self.title,font=("times new roman",15,"bold"),bd=2,relief=GROOVE)
self.titlebar.pack(side=TOP,fill=BOTH)
self.settitle()
# Creating Statusbar
self.statusbar = Label(self.root,textvariable=self.status,font=("times new roman",15,"bold"),bd=2,relief=GROOVE)
self.statusbar.pack(side=BOTTOM,fill=BOTH)
self.status.set("Welcome To Text Editor")
# Creating Menubar
self.menubar = Menu(self.root,font=("times new roman",15,"bold"),activebackground="skyblue")
self.root.config(menu=self.menubar)
self.filemenu = Menu(self.menubar,font=("times new roman",12,"bold"),activebackground="skyblue",tearoff=0)
self.filemenu.add_command(label="New",accelerator="Ctrl+N",command=self.newfile)
self.filemenu.add_command(label="Open",accelerator="Ctrl+O",command=self.openfile)
self.filemenu.add_command(label="Save",accelerator="Ctrl+S",command=self.savefile)
self.filemenu.add_command(label="Save As",accelerator="Ctrl+A",command=self.saveasfile)
self.filemenu.add_separator()
self.filemenu.add_command(label="Exit",accelerator="Ctrl+E",command=self.exit)
self.menubar.add_cascade(label="File", menu=self.filemenu)
self.editmenu = Menu(self.menubar,font=("times new roman",12,"bold"),activebackground="skyblue",tearoff=0)
self.editmenu.add_command(label="Cut",accelerator="Ctrl+X",command=self.cut)
self.editmenu.add_command(label="Copy",accelerator="Ctrl+C",command=self.copy)
self.editmenu.add_command(label="Paste",accelerator="Ctrl+V",command=self.paste)
self.editmenu.add_separator()
self.editmenu.add_command(label="Undo",accelerator="Ctrl+U",command=self.undo)
self.menubar.add_cascade(label="Edit", menu=self.editmenu)
# Creating Help Menu
self.helpmenu = Menu(self.menubar,font=("times new roman",12,"bold"),activebackground="skyblue",tearoff=0)
self.helpmenu.add_command(label="About",command=self.infoabout)
self.menubar.add_cascade(label="Help", menu=self.helpmenu)
scrol_y = Scrollbar(self.root,orient=VERTICAL)
self.txtarea = Text(self.root,yscrollcommand=scrol_y.set,font=("times new roman",15,"bold"),state="normal",relief=GROOVE)
scrol_y.pack(side=RIGHT,fill=Y)
scrol_y.config(command=self.txtarea.yview)
self.txtarea.pack(fill=BOTH,expand=1)
self.shortcuts()
def settitle(self):
if self.filename:
self.title.set(self.filename)
else:
self.title.set("Untitled")
def newfile(self,*args):
self.txtarea.delete("1.0",END)
self.filename = None
self.settitle()
self.status.set("New File Created")
def openfile(self,*args):
try:
self.filename = filedialog.askopenfilename(title = "Select file",filetypes = (("All Files","*.*"),("Text Files","*.txt"),("Python Files","*.py")))
# checking if filename not none
if self.filename:
infile = open(self.filename,"r")
self.txtarea.delete("1.0",END)
# Inserting data Line by line into text area
for line in infile:
self.txtarea.insert(END,line)
infile.close()
self.settitle()
self.status.set("Opened Successfully")
except Exception as e:
messagebox.showerror("Exception",e)
# Defining Save File Funtion
def savefile(self,*args):
# Exception handling
try:
if self.filename:
data = self.txtarea.get("1.0",END)
outfile = open(self.filename,"w")
outfile.write(data)
outfile.close()
self.settitle()
self.status.set("Saved Successfully")
else:
self.saveasfile()
except Exception as e:
messagebox.showerror("Exception",e)
# Defining Save As File Funtion
def saveasfile(self,*args):
try:
untitledfile = filedialog.asksaveasfilename(title = "Save file As",defaultextension=".txt",initialfile = "Untitled.txt",filetypes = (("All Files","*.*"),("Text Files","*.txt"),("Python Files","*.py")))
data = self.txtarea.get("1.0",END)
outfile = open(untitledfile,"w")
outfile.write(data)
outfile.close()
self.filename = untitledfile
self.settitle()
self.status.set("Saved Successfully")
except Exception as e:
messagebox.showerror("Exception",e)
def exit(self,*args):
op = messagebox.askyesno("WARNING","Your Unsaved Data May be Lost!!")
if op>0:
self.root.destroy()
else:
return
# Defining Cut Funtion
def cut(self,*args):
self.txtarea.event_generate("<<Cut>>")
# Defining Copy Funtion
def copy(self,*args):
self.txtarea.event_generate("<<Copy>>")
# Defining Paste Funtion
def paste(self,*args):
self.txtarea.event_generate("<<Paste>>")
# Defining Undo Funtion
def undo(self,*args):
try:
if self.filename:
self.txtarea.delete("1.0",END)
infile = open(self.filename,"r")
for line in infile:
self.txtarea.insert(END,line)
infile.close()
self.settitle()
self.status.set("Undone Successfully")
else:
# Clearing Text Area
self.txtarea.delete("1.0",END)
# Updating filename as None
self.filename = None
# Calling Set title
self.settitle()
# Updating Status
self.status.set("Undone Successfully")
except Exception as e:
messagebox.showerror("Exception",e)
# Defining About Funtion
def infoabout(self):
messagebox.showinfo("About Text Editor","A Simple Text Editor\nCreated using Python.")
# Defining shortcuts Funtion
def shortcuts(self):
self.txtarea.bind("<Control-n>",self.newfile)
self.txtarea.bind("<Control-o>",self.openfile)
self.txtarea.bind("<Control-s>",self.savefile)
self.txtarea.bind("<Control-a>",self.saveasfile)
self.txtarea.bind("<Control-e>",self.exit)
self.txtarea.bind("<Control-x>",self.cut)
self.txtarea.bind("<Control-c>",self.copy)
self.txtarea.bind("<Control-v>",self.paste)
self.txtarea.bind("<Control-u>",self.undo)
# Creating TK Container
TextEditor(root)
root.mainloop()