python how to make notepad

Solutions on MaxInterview for python how to make notepad by the best coders in the world

showing results for - "python how to make notepad"
Tommaso
29 Apr 2020
1import tkinter
2import os	
3from tkinter import *
4from tkinter.messagebox import *
5from tkinter.filedialog import *
6
7class Notepad:
8
9	__root = Tk()
10
11	# default window width and height
12	__thisWidth = 300
13	__thisHeight = 300
14	__thisTextArea = Text(__root)
15	__thisMenuBar = Menu(__root)
16	__thisFileMenu = Menu(__thisMenuBar, tearoff=0)
17	__thisEditMenu = Menu(__thisMenuBar, tearoff=0)
18	__thisHelpMenu = Menu(__thisMenuBar, tearoff=0)
19	
20	# To add scrollbar
21	__thisScrollBar = Scrollbar(__thisTextArea)	
22	__file = None
23
24	def __init__(self,**kwargs):
25
26		# Set icon
27		try:
28				self.__root.wm_iconbitmap("Notepad.ico")
29		except:
30				pass
31
32		# Set window size (the default is 300x300)
33
34		try:
35			self.__thisWidth = kwargs['width']
36		except KeyError:
37			pass
38
39		try:
40			self.__thisHeight = kwargs['height']
41		except KeyError:
42			pass
43
44		# Set the window text
45		self.__root.title("Untitled - Notepad")
46
47		# Center the window
48		screenWidth = self.__root.winfo_screenwidth()
49		screenHeight = self.__root.winfo_screenheight()
50	
51		# For left-alling
52		left = (screenWidth / 2) - (self.__thisWidth / 2)
53		
54		# For right-allign
55		top = (screenHeight / 2) - (self.__thisHeight /2)
56		
57		# For top and bottom
58		self.__root.geometry('%dx%d+%d+%d' % (self.__thisWidth,
59											self.__thisHeight,
60											left, top))
61
62		# To make the textarea auto resizable
63		self.__root.grid_rowconfigure(0, weight=1)
64		self.__root.grid_columnconfigure(0, weight=1)
65
66		# Add controls (widget)
67		self.__thisTextArea.grid(sticky = N + E + S + W)
68		
69		# To open new file
70		self.__thisFileMenu.add_command(label="New",
71										command=self.__newFile)	
72		
73		# To open a already existing file
74		self.__thisFileMenu.add_command(label="Open",
75										command=self.__openFile)
76		
77		# To save current file
78		self.__thisFileMenu.add_command(label="Save",
79										command=self.__saveFile)	
80
81		# To create a line in the dialog		
82		self.__thisFileMenu.add_separator()										
83		self.__thisFileMenu.add_command(label="Exit",
84										command=self.__quitApplication)
85		self.__thisMenuBar.add_cascade(label="File",
86									menu=self.__thisFileMenu)	
87		
88		# To give a feature of cut
89		self.__thisEditMenu.add_command(label="Cut",
90										command=self.__cut)			
91	
92		# to give a feature of copy	
93		self.__thisEditMenu.add_command(label="Copy",
94										command=self.__copy)		
95		
96		# To give a feature of paste
97		self.__thisEditMenu.add_command(label="Paste",
98										command=self.__paste)		
99		
100		# To give a feature of editing
101		self.__thisMenuBar.add_cascade(label="Edit",
102									menu=self.__thisEditMenu)	
103		
104		# To create a feature of description of the notepad
105		self.__thisHelpMenu.add_command(label="About Notepad",
106										command=self.__showAbout)
107		self.__thisMenuBar.add_cascade(label="Help",
108									menu=self.__thisHelpMenu)
109
110		self.__root.config(menu=self.__thisMenuBar)
111
112		self.__thisScrollBar.pack(side=RIGHT,fill=Y)					
113		
114		# Scrollbar will adjust automatically according to the content		
115		self.__thisScrollBar.config(command=self.__thisTextArea.yview)	
116		self.__thisTextArea.config(yscrollcommand=self.__thisScrollBar.set)
117	
118		
119	def __quitApplication(self):
120		self.__root.destroy()
121		# exit()
122
123	def __showAbout(self):
124		showinfo("Notepad","Mrinal Verma")
125
126	def __openFile(self):
127		
128		self.__file = askopenfilename(defaultextension=".txt",
129									filetypes=[("All Files","*.*"),
130										("Text Documents","*.txt")])
131
132		if self.__file == "":
133			
134			# no file to open
135			self.__file = None
136		else:
137			
138			# Try to open the file
139			# set the window title
140			self.__root.title(os.path.basename(self.__file) + " - Notepad")
141			self.__thisTextArea.delete(1.0,END)
142
143			file = open(self.__file,"r")
144
145			self.__thisTextArea.insert(1.0,file.read())
146
147			file.close()
148
149		
150	def __newFile(self):
151		self.__root.title("Untitled - Notepad")
152		self.__file = None
153		self.__thisTextArea.delete(1.0,END)
154
155	def __saveFile(self):
156
157		if self.__file == None:
158			# Save as new file
159			self.__file = asksaveasfilename(initialfile='Untitled.txt',
160											defaultextension=".txt",
161											filetypes=[("All Files","*.*"),
162												("Text Documents","*.txt")])
163
164			if self.__file == "":
165				self.__file = None
166			else:
167				
168				# Try to save the file
169				file = open(self.__file,"w")
170				file.write(self.__thisTextArea.get(1.0,END))
171				file.close()
172				
173				# Change the window title
174				self.__root.title(os.path.basename(self.__file) + " - Notepad")
175				
176			
177		else:
178			file = open(self.__file,"w")
179			file.write(self.__thisTextArea.get(1.0,END))
180			file.close()
181
182	def __cut(self):
183		self.__thisTextArea.event_generate("<<Cut>>")
184
185	def __copy(self):
186		self.__thisTextArea.event_generate("<<Copy>>")
187
188	def __paste(self):
189		self.__thisTextArea.event_generate("<<Paste>>")
190
191	def run(self):
192
193		# Run main application
194		self.__root.mainloop()
195
196
197
198
199# Run main application
200notepad = Notepad(width=600,height=400)
201notepad.run()
202