-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_editor.py
152 lines (117 loc) · 4.87 KB
/
text_editor.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
import tkinter
import os
from tkinter import *
from tkinter.messagebox import *
from tkinter.filedialog import *
class Notepad:
root = Tk()
thisWidth = 500
thisHeight = 300
thisTextArea = Text(root)
thisMenuBar = Menu(root)
thisFileMenu = Menu(thisMenuBar, tearoff=0)
thisEditMenu = Menu(thisMenuBar, tearoff=0)
thisHelpMenu = Menu(thisMenuBar, tearoff=0)
# To add scrollbar
thisScrollBar = Scrollbar(thisTextArea)
file = None
def __init__(self, **kwargs):
# Set icon
try:
self.root.wm_iconbitmap("Notepad.ico")
except:
pass
# Set window size (the default is 500x300)
try:
self.__thisWidth = kwargs['width']
except KeyError:
pass
try:
self.__thisHeight = kwargs['height']
except KeyError:
pass
# Set the window text and set the window title
self.root.title("Untitled - Notepad")
# Center the window
screenWidth = self.root.winfo_screenwidth()
screenHeight = self.root.winfo_screenheight()
# For left-alling
left = (screenWidth / 2) - (self.thisWidth / 2)
# For right-allign
top = (screenHeight / 2) - (self.thisHeight / 2)
# For top and bottom
self.root.geometry('%dx%d+%d+%d' % (self.thisWidth, self.thisHeight, left, top))
# To make the textarea auto resizable
self.root.grid_rowconfigure(0, weight=1)
self.root.grid_columnconfigure(0, weight=1)
# Add controls (widget)
self.thisTextArea.grid(sticky=N + E + S + W)
# File menu list
self.thisMenuBar.add_cascade(label="File", menu=self.thisFileMenu)
self.thisFileMenu.add_command(label="New", command=self.newFile)
self.thisFileMenu.add_command(label="Open", command=self.openFile)
self.thisFileMenu.add_command(label="Save", command=self.saveFile)
# To create a line in the dialog
self.thisFileMenu.add_separator()
self.thisFileMenu.add_command(label="Exit", command=self.quitApplication)
# To give a feature of editing
self.thisMenuBar.add_cascade(label="Edit", menu=self.thisEditMenu)
self.thisEditMenu.add_command(label="Cut", command=self.cut)
self.thisEditMenu.add_command(label="Copy", command=self.copy)
self.thisEditMenu.add_command(label="Paste", command=self.paste)
# To create a feature of description of the notepad
self.thisMenuBar.add_cascade(label="Help", menu=self.thisHelpMenu)
self.thisHelpMenu.add_command(label="About Notepad", command=self.showAbout)
self.root.config(menu=self.thisMenuBar)
self.thisScrollBar.pack(side=RIGHT, fill=Y)
# Scrollbar will adjust automatically according to the content
self.thisScrollBar.config(command=self.thisTextArea.yview)
self.thisTextArea.config(yscrollcommand=self.thisScrollBar.set)
def quitApplication(self):
self.root.destroy()
# exit()
def showAbout(self):
showinfo("Notepad", "Deepak Gupta")
def openFile(self):
self.file = askopenfilename(defaultextension=".txt", filetypes=[("All Files", "*.*"),
("Text Documents", "*.txt")])
if self.file == "":
self.file = None # no file to open
else:
# Try to open the file
self.root.title(os.path.basename(self.file) + " - Notepad")
self.thisTextArea.delete(1.0, END)
file = open(self.file, "r")
self.thisTextArea.insert(1.0, file.read())
file.close()
def newFile(self):
self.root.title("Untitled - Notepad")
self.file = None
self.thisTextArea.delete(1.0, END)
def saveFile(self):
# Save as new file
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") # Try to save the file
file.write(self.thisTextArea.get(1.0, END))
file.close()
self.root.title(os.path.basename(self.file) + " - Notepad") # Change the window title
else:
file = open(self.file, "w")
file.write(self.thisTextArea.get(1.0, END))
file.close()
def cut(self):
self.thisTextArea.event_generate("<<Cut>>")
def copy(self):
self.thisTextArea.event_generate("<<Copy>>")
def paste(self):
self.thisTextArea.event_generate("<<Paste>>")
# Run main application
def run(self):
self.root.mainloop()
notepad = Notepad(width=600, height=400)
notepad.run()