1+ from tkinter import *
2+ import tkinter as tk
3+ from tkinter .scrolledtext import ScrolledText
4+ from tkinter import filedialog
5+ from tkinter import messagebox ,font
6+ from tkinter import ttk
7+ from datetime import datetime
8+ import webbrowser
9+
10+
11+ #======================================================================================
12+ # ========================== File Code Starts Here ============================
13+ #=======================================================================================
14+
15+
16+ #=================================== New Code ======================================
17+ def new ():
18+ text .delete ('1.0' ,'end' )
19+ #===================================== End =========================================
20+
21+
22+ # ========================= New Window Code ================================
23+ def new_window ():
24+ root = tk .Tk ()
25+ root .geometry ('500x500' )
26+
27+
28+ menubar = Menu (root )
29+
30+ file = Menu (menubar ,tearoff = 0 )
31+ file .add_command (label = "New" ,command = new )
32+ file .add_command (label = "New window" ,command = new_window )
33+ file .add_command (label = "Open" ,command = Open )
34+ file .add_command (label = "Save" ,command = save )
35+ file .add_command (label = "Save as" , command = save_as )
36+ file .add_separator ()
37+ file .add_command (label = "Exit" ,command = exit )
38+ menubar .add_cascade (label = "File" ,menu = file ,font = ('verdana' ,10 ,'bold' ))
39+
40+
41+
42+ edit = Menu (menubar ,tearoff = 0 )
43+
44+ edit .add_command (label = "Undo" ,command = undo )
45+ edit .add_separator ()
46+ edit .add_command (label = "Cut" ,command = cut )
47+ edit .add_command (label = "Copy" ,command = copy )
48+ edit .add_command (label = "Paste" ,command = paste )
49+ edit .add_command (label = "Delete" ,command = delete )
50+ edit .add_command (label = "Select All" ,accelerator = "Ctrl+A" ,command = select_all )
51+ edit .add_command (label = "Time/Date" ,accelerator = "F5" ,command = time )
52+ menubar .add_cascade (label = "Edit" ,menu = edit )
53+
54+
55+ Format = Menu (menubar , tearoff = 0 )
56+
57+ Format .add_command (label = "Word Wrap" )
58+ Format .add_command (label = "Font..." , command = fonts )
59+
60+ menubar .add_cascade (label = "Format" ,menu = Format )
61+
62+
63+
64+ Help = Menu (menubar , tearoff = 0 )
65+
66+ Help .add_command (label = "View Help" ,command = view_help )
67+ Help .add_command (label = "Send FeedBack" ,command = send_feedback )
68+ Help .add_command (label = "About Notepad" )
69+
70+ menubar .add_cascade (label = "Help" ,menu = Help )
71+
72+
73+ root .config (menu = menubar )
74+
75+
76+
77+
78+
79+ text = ScrolledText (root ,width = 1000 ,height = 1000 )
80+ text .place (x = 0 ,y = 0 )
81+
82+
83+
84+ root .mainloop ()
85+
86+ # =========================== End ==============================================
87+
88+
89+ # ===================== Open File Code ========================================
90+ def Open ():
91+ root .filename = filedialog .askopenfilename (
92+ initialdir = '/' ,
93+ title = "Select file" ,
94+ filetypes = (("jpeg files" ,"*.jpg" ),("all files" ,"*.*" )))
95+ file = open (root .filename )
96+ text .insert ('end' ,file .read ())
97+ #================================= End ==========================================
98+
99+
100+ #================================ Save File Code ====================================
101+ def save ():
102+ pass
103+ #================================ End =======================================
104+
105+ #=================================== save as File code ==============================
106+ def save_as ():
107+ root .filename = filedialog .asksaveasfile (mode = "w" ,defaultextension = '.txt' )
108+ if root .filename is None :
109+ return
110+ file_save = str (text .get (1.0 ,END ))
111+ root .filename .write (file_save )
112+ root .filename .close ()
113+ # ================================ End ============================================
114+
115+ # ================================ Exit Code =====================================
116+ def exit ():
117+ message = messagebox .askquestion ('Notepad' ,"Do you want to save changes" )
118+ if message == "yes" :
119+ save_as ()
120+ else :
121+ root .destroy ()
122+ #==================================== end =========================================
123+
124+
125+
126+
127+ #======================================================================================
128+ # ======================= Edit Code Starts Here ============================
129+ #=======================================================================================
130+
131+
132+
133+
134+ #=========================== Undo code =============================
135+ def undo ():
136+ pass
137+ # Its Challenge for you to give the undo functionality in the notepad
138+
139+ #=========================== End code =====================================
140+
141+ #=========================== Cut code =============================
142+ def cut ():
143+ text .event_generate ("<<Cut>>" )
144+
145+ #=========================== End code =====================================
146+
147+ #=========================== Cut code =============================
148+ def copy ():
149+ text .event_generate ("<<Copy>>" )
150+
151+ #=========================== End code =====================================
152+
153+ #=========================== Cut code =============================
154+ def paste ():
155+ text .event_generate ("<<Paste>>" )
156+
157+ #=========================== End code =====================================
158+
159+
160+ #=========================== Delete all code =============================
161+ def delete ():
162+ message = messagebox .askquestion ('Notepad' ,"Do you want to Delete all" )
163+ if message == "yes" :
164+ text .delete ('1.0' ,'end' )
165+ else :
166+ return "break"
167+
168+
169+ #=========================== End code =====================================
170+
171+
172+ #=========================== select all code =============================
173+ def select_all ():
174+ text .tag_add ('sel' ,'1.0' ,'end' )
175+ return 'break'
176+ #=========================== End code =============================
177+
178+
179+ #=========================== Time/Date code =============================
180+ def time ():
181+ d = datetime .now ()
182+ text .insert ('end' ,d )
183+
184+ #=========================== End code =============================
185+
186+
187+
188+ #======================================================================================
189+ # ======================= Edit Code Ends Here ============================
190+ #=======================================================================================
191+
192+
193+
194+
195+ #======================================================================================
196+ # ======================= Format Code Starts Here ============================
197+ #=======================================================================================
198+
199+
200+ def fonts ():
201+ root = tk .Tk ()
202+ root .geometry ('400x400' )
203+ root .title ('Font' )
204+
205+ l1 = Label (root ,text = "Font:" )
206+ l1 .place (x = 10 ,y = 10 )
207+ f = tk .StringVar ()
208+ fonts = ttk .Combobox (root , width = 15 , textvariable = f , state = 'readonly' ,font = ('verdana' ,10 ,'bold' ),)
209+ fonts ['values' ] = font .families ()
210+ fonts .place (x = 10 ,y = 30 )
211+ fonts .current (0 )
212+
213+
214+ l2 = Label (root ,text = "Font Style:" )
215+ l2 .place (x = 180 ,y = 10 )
216+ st = tk .StringVar ()
217+ style = ttk .Combobox (root , width = 15 , textvariable = st , state = 'readonly' ,font = ('verdana' ,10 ,'bold' ),)
218+ style ['values' ] = ('bold' ,'bold italic' ,'italic' )
219+ style .place (x = 180 ,y = 30 )
220+ style .current (0 )
221+
222+ l3 = Label (root ,text = "Size:" )
223+ l3 .place (x = 350 ,y = 10 )
224+ sz = tk .StringVar ()
225+ size = ttk .Combobox (root , width = 2 , textvariable = sz , state = 'readonly' ,font = ('verdana' ,10 ,'bold' ),)
226+
227+ size ['values' ] = (8 ,9 ,10 ,12 ,15 ,20 ,23 ,25 ,27 ,30 ,35 ,40 ,43 ,47 ,50 ,55 ,65 ,76 ,80 ,90 ,100 ,150 ,200 ,255 ,300 )
228+ size .place (x = 350 ,y = 30 )
229+ size .current (0 )
230+
231+
232+ sample = LabelFrame (root ,text = "Sample" ,height = 100 ,width = 200 )
233+ sample ['font' ] = (fonts .get (),size .get (),style .get ())
234+ sample .place (x = 180 ,y = 220 )
235+
236+ l4 = Label (sample ,text = "This is sample" )
237+ l4 .place (x = 20 ,y = 30 )
238+
239+
240+
241+ def OK ():
242+
243+ text ['font' ] = (fonts .get (),size .get (),style .get ())
244+ root .destroy ()
245+
246+
247+ ok = Button (root ,text = "OK" ,relief = RIDGE ,borderwidth = 2 ,padx = 20 ,highlightcolor = "blue" ,command = OK )
248+ ok .place (x = 137 ,y = 350 )
249+
250+ def Apl ():
251+ l4 ['font' ] = (fonts .get (),size .get (),style .get ())
252+
253+ Apply = Button (root ,text = "Apply" ,relief = RIDGE ,borderwidth = 2 ,padx = 20 ,highlightcolor = "blue" ,command = Apl )
254+ Apply .place (x = 210 ,y = 350 )
255+
256+ def Cnl ():
257+ root .destroy ()
258+
259+ cancel = Button (root ,text = "Cancel" ,relief = RIDGE ,borderwidth = 2 ,padx = 20 ,command = Cnl )
260+ cancel .place (x = 295 ,y = 350 )
261+ root .mainloop ()
262+
263+
264+ #======================================================================================
265+ # ======================= Format Code Ends Here ============================
266+ #=======================================================================================
267+
268+ #======================================================================================
269+ # ======================= Help Code Ends Here ============================
270+ #=======================================================================================
271+
272+ # ====================== View Help ===================================
273+ def view_help ():
274+ webbrowser .open ('#' )
275+
276+ #============================= End =======================================
277+
278+ # ====================== View Help ===================================
279+ def send_feedback ():
280+ webbrowser .open ('#' )
281+
282+ #============================= End =======================================
283+
284+
285+ #======================================================================================
286+ # ======================= Help Code Ends Here ============================
287+ #=======================================================================================
288+
289+
290+ # ============================= Main Window =============================
291+
292+ root = tk .Tk ()
293+ root .geometry ('500x500' )
294+ root .title ('notepad' )
295+ root .iconbitmap ('notepad.ico' )
296+ menubar = Menu (root )
297+
298+ file = Menu (menubar ,tearoff = 0 )
299+ file .add_command (label = "New" ,command = new )
300+ file .add_command (label = "New window" ,command = new_window )
301+ file .add_command (label = "Open" ,command = Open )
302+ file .add_command (label = "Save" ,command = save )
303+ file .add_command (label = "Save as" , command = save_as )
304+ file .add_separator ()
305+ file .add_command (label = "Exit" ,command = exit )
306+ menubar .add_cascade (label = "File" ,menu = file ,font = ('verdana' ,10 ,'bold' ))
307+
308+
309+
310+ edit = Menu (menubar ,tearoff = 0 )
311+
312+ edit .add_command (label = "Undo" ,accelerator = "Ctrl+Z" ,command = undo )
313+ edit .add_separator ()
314+ edit .add_command (label = "Cut" ,accelerator = "Ctrl+X" ,command = cut )
315+ edit .add_command (label = "Copy" ,accelerator = "Ctrl+C" ,command = copy )
316+ edit .add_command (label = "Paste" ,accelerator = "Ctrl+V" ,command = paste )
317+ edit .add_command (label = "Delete" ,accelerator = "Del" ,command = delete )
318+ edit .add_command (label = "Select All" ,accelerator = "Ctrl+A" ,command = select_all )
319+ edit .add_command (label = "Time/Date" ,accelerator = "F5" ,command = time )
320+ menubar .add_cascade (label = "Edit" ,menu = edit )
321+
322+
323+ Format = Menu (menubar , tearoff = 0 )
324+
325+ Format .add_command (label = "Word Wrap" )
326+ Format .add_command (label = "Font..." , command = fonts )
327+
328+ menubar .add_cascade (label = "Format" ,menu = Format )
329+
330+
331+
332+ Help = Menu (menubar , tearoff = 0 )
333+
334+ Help .add_command (label = "View Help" ,command = view_help )
335+ Help .add_command (label = "Send FeedBack" ,command = send_feedback )
336+ Help .add_command (label = "About Notepad" )
337+
338+ menubar .add_cascade (label = "Help" ,menu = Help )
339+
340+
341+ root .config (menu = menubar )
342+
343+
344+
345+
346+
347+ text = ScrolledText (root ,width = 1000 ,height = 1000 )
348+ text .place (x = 0 ,y = 0 )
349+
350+
351+
352+ root .mainloop ()
353+
354+ # ========================== End =======================================
0 commit comments