Welcome folks today in this blog post we will be making a windows notepad
in tkinter using python. This will be a complete notepad where you can perform the following operations on it
create new files
save files
And now we need to install python
on our system.
And after this we need to install tkinter
on your system by executing the following command
pip install tkinter
And after this make a notepad.py
file and copy paste the following code
notepad.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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
from tkinter import * import tkinter as tk from tkinter.scrolledtext import ScrolledText from tkinter import filedialog from tkinter import messagebox,font from tkinter import ttk from datetime import datetime import webbrowser #====================================================================================== # ========================== File Code Starts Here ============================ #======================================================================================= #=================================== New Code ====================================== def new(): text.delete('1.0','end') #===================================== End ========================================= # ========================= New Window Code ================================ def new_window(): root = tk.Tk() root.geometry('500x500') menubar = Menu(root) file = Menu(menubar,tearoff = 0) file.add_command(label="New",command=new) file.add_command(label="New window",command=new_window) file.add_command(label="Open",command=Open) file.add_command(label="Save",command=save) file.add_command(label="Save as", command=save_as) file.add_separator() file.add_command(label="Exit",command=exit) menubar.add_cascade(label="File",menu=file,font=('verdana',10,'bold')) edit = Menu(menubar,tearoff = 0) edit.add_command(label="Undo",command=undo) edit.add_separator() edit.add_command(label="Cut",command=cut) edit.add_command(label="Copy",command=copy) edit.add_command(label="Paste",command=paste) edit.add_command(label="Delete",command=delete) edit.add_command(label="Select All",accelerator="Ctrl+A",command=select_all) edit.add_command(label="Time/Date",accelerator="F5",command=time) menubar.add_cascade(label="Edit",menu=edit) Format = Menu(menubar, tearoff = 0) Format.add_command(label="Word Wrap") Format.add_command(label="Font...", command=fonts) menubar.add_cascade(label="Format",menu=Format) Help = Menu(menubar, tearoff = 0) Help.add_command(label="View Help",command=view_help) Help.add_command(label="Send FeedBack",command=send_feedback) Help.add_command(label="About Notepad") menubar.add_cascade(label="Help",menu=Help) root.config(menu=menubar) text = ScrolledText(root,width=1000,height=1000) text.place(x=0,y=0) root.mainloop() # =========================== End ============================================== # ===================== Open File Code ======================================== def Open(): root.filename = filedialog.askopenfilename( initialdir = '/', title="Select file", filetypes=(("jpeg files","*.jpg"),("all files","*.*"))) file = open(root.filename) text.insert('end',file.read()) #================================= End ========================================== #================================ Save File Code ==================================== def save(): pass #================================ End ======================================= #=================================== save as File code ============================== def save_as(): root.filename = filedialog.asksaveasfile(mode="w",defaultextension='.txt') if root.filename is None: return file_save = str(text.get(1.0,END)) root.filename.write(file_save) root.filename.close() # ================================ End ============================================ # ================================ Exit Code ===================================== def exit(): message = messagebox.askquestion('Notepad',"Do you want to save changes") if message == "yes": save_as() else: root.destroy() #==================================== end ========================================= #====================================================================================== # ======================= Edit Code Starts Here ============================ #======================================================================================= #=========================== Cut code ============================= def cut(): text.event_generate("<<Cut>>") #=========================== End code ===================================== #=========================== Cut code ============================= def copy(): text.event_generate("<<Copy>>") #=========================== End code ===================================== #=========================== Cut code ============================= def paste(): text.event_generate("<<Paste>>") #=========================== End code ===================================== #=========================== Delete all code ============================= def delete(): message = messagebox.askquestion('Notepad',"Do you want to Delete all") if message == "yes": text.delete('1.0','end') else: return "break" #=========================== End code ===================================== #=========================== select all code ============================= def select_all(): text.tag_add('sel','1.0','end') return 'break' #=========================== End code ============================= #=========================== Time/Date code ============================= def time(): d = datetime.now() text.insert('end',d) #=========================== End code ============================= #====================================================================================== # ======================= Edit Code Ends Here ============================ #======================================================================================= #====================================================================================== # ======================= Format Code Starts Here ============================ #======================================================================================= def fonts(): root = tk.Tk() root.geometry('400x400') root.title('Font') l1 = Label(root,text="Font:") l1.place(x=10,y=10) f = tk.StringVar() fonts = ttk.Combobox(root, width = 15, textvariable = f, state='readonly',font=('verdana',10,'bold'),) fonts['values'] = font.families() fonts.place(x=10,y=30) fonts.current(0) l2 = Label(root,text="Font Style:") l2.place(x=180,y=10) st = tk.StringVar() style = ttk.Combobox(root, width = 15, textvariable = st, state='readonly',font=('verdana',10,'bold'),) style['values'] = ('bold','bold italic','italic') style.place(x=180,y=30) style.current(0) l3 = Label(root,text="Size:") l3.place(x=350,y=10) sz = tk.StringVar() size = ttk.Combobox(root, width = 2, textvariable = sz, state='readonly',font=('verdana',10,'bold'),) 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) size.place(x=350,y=30) size.current(0) sample = LabelFrame(root,text="Sample",height=100,width=200) sample['font'] = (fonts.get(),size.get(),style.get()) sample.place(x=180,y=220) l4 = Label(sample,text="This is sample") l4.place(x=20,y=30) def OK(): text['font'] = (fonts.get(),size.get(),style.get()) root.destroy() ok = Button(root,text="OK",relief=RIDGE,borderwidth=2,padx=20,highlightcolor="blue",command=OK) ok.place(x=137,y=350) def Apl(): l4['font'] = (fonts.get(),size.get(),style.get()) Apply = Button(root,text="Apply",relief=RIDGE,borderwidth=2,padx=20,highlightcolor="blue",command=Apl) Apply.place(x=210,y=350) def Cnl(): root.destroy() cancel = Button(root,text="Cancel",relief=RIDGE,borderwidth=2,padx=20,command=Cnl) cancel.place(x=295,y=350) root.mainloop() #====================================================================================== # ======================= Format Code Ends Here ============================ #======================================================================================= #====================================================================================== # ======================= Help Code Ends Here ============================ #======================================================================================= # ====================== View Help =================================== def view_help(): webbrowser.open('#') #============================= End ======================================= # ====================== View Help =================================== def send_feedback(): webbrowser.open('#') #============================= End ======================================= #====================================================================================== # ======================= Help Code Ends Here ============================ #======================================================================================= # ============================= Main Window ============================= root = tk.Tk() root.geometry('600x300') root.minsize(200,100) root.title('notepad') root.iconbitmap('notepad.ico') text = ScrolledText(root,height=1000,undo=True) text.pack(fill=tk.BOTH) menubar = Menu(root) file = Menu(menubar,tearoff = 0) file.add_command(label="New",command=new) file.add_command(label="New window",command=new_window) file.add_command(label="Open",command=Open) file.add_command(label="Save",command=save) file.add_command(label="Save as", command=save_as) file.add_separator() file.add_command(label="Exit",command=exit) menubar.add_cascade(label="File",menu=file,font=('verdana',10,'bold')) edit = Menu(menubar,tearoff = 0) edit.add_command(label="Undo",accelerator="Ctrl+Z",command=text.edit_undo) edit.add_command(label="Redo",accelerator="Ctrl+Y",command=text.edit_redo) edit.add_separator() edit.add_command(label="Cut",accelerator="Ctrl+X",command=cut) edit.add_command(label="Copy",accelerator="Ctrl+C",command=copy) edit.add_command(label="Paste",accelerator="Ctrl+V",command=paste) edit.add_command(label="Delete",accelerator="Del",command=delete) edit.add_command(label="Select All",accelerator="Ctrl+A",command=select_all) edit.add_command(label="Time/Date",accelerator="F5",command=time) menubar.add_cascade(label="Edit",menu=edit) Format = Menu(menubar, tearoff = 0) Format.add_command(label="Word Wrap") Format.add_command(label="Font...", command=fonts) menubar.add_cascade(label="Format",menu=Format) Help = Menu(menubar, tearoff = 0) Help.add_command(label="View Help",command=view_help) Help.add_command(label="Send FeedBack",command=send_feedback) Help.add_command(label="About Notepad") menubar.add_cascade(label="Help",menu=Help) # ======================== Right Click Menu ========================================= m = Menu(root, tearoff = 0) m.add_command(label ="Select All",accelerator="Ctrl+A",command=select_all) m.add_command(label ="Cut",accelerator="Ctrl+X",command=cut) m.add_command(label ="Copy",accelerator="Ctrl+C",command=copy) m.add_command(label ="Paste",accelerator="Ctrl+V",command=paste) m.add_command(label ="Delete",accelerator="Del",command=delete) m.add_separator() m.add_command(label ="Undo",accelerator="Ctrl+Z",command=text.edit_undo) m.add_command(label ="Redo",accelerator="Ctrl+Z",command=text.edit_redo) def do_popup(event): try: m.tk_popup(event.x_root, event.y_root) finally: m.grab_release() root.bind("<Button-3>", do_popup) # ============================================================================== root.config(menu=menubar) root.mainloop() # ========================== End ======================================= |
Screenshot