|
| 1 | +import tkinter |
| 2 | +import os |
| 3 | +from tkinter import * |
| 4 | +from tkinter.messagebox import * |
| 5 | +from tkinter.filedialog import * |
| 6 | + |
| 7 | + |
| 8 | +class Notepad: |
| 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 | + def __quitApplication(self): |
| 119 | + self.__root.destroy() |
| 120 | + |
| 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 | + def __newFile(self): |
| 150 | + self.__root.title("Untitled - Notepad") |
| 151 | + self.__file = None |
| 152 | + self.__thisTextArea.delete(1.0, END) |
| 153 | + |
| 154 | + def __saveFile(self): |
| 155 | + |
| 156 | + if self.__file == None: |
| 157 | + # Save as new file |
| 158 | + self.__file = asksaveasfilename(initialfile='Untitled.txt', |
| 159 | + defaultextension=".txt", |
| 160 | + filetypes=[("All Files", "*.*"), |
| 161 | + ("Text Documents", "*.txt")]) |
| 162 | + |
| 163 | + if self.__file == "": |
| 164 | + self.__file = None |
| 165 | + else: |
| 166 | + |
| 167 | + # Try to save the file |
| 168 | + file = open(self.__file, "w") |
| 169 | + file.write(self.__thisTextArea.get(1.0, END)) |
| 170 | + file.close() |
| 171 | + |
| 172 | + # Change the window title |
| 173 | + self.__root.title(os.path.basename(self.__file) + " - Notepad") |
| 174 | + |
| 175 | + |
| 176 | + else: |
| 177 | + file = open(self.__file, "w") |
| 178 | + file.write(self.__thisTextArea.get(1.0, END)) |
| 179 | + file.close() |
| 180 | + |
| 181 | + def __cut(self): |
| 182 | + self.__thisTextArea.event_generate("<<Cut>>") |
| 183 | + |
| 184 | + def __copy(self): |
| 185 | + self.__thisTextArea.event_generate("<<Copy>>") |
| 186 | + |
| 187 | + def __paste(self): |
| 188 | + self.__thisTextArea.event_generate("<<Paste>>") |
| 189 | + |
| 190 | + def run(self): |
| 191 | + |
| 192 | + # Run main application |
| 193 | + self.__root.mainloop() |
| 194 | + |
| 195 | + # Run main application |
| 196 | + |
| 197 | + |
| 198 | +notepad = Notepad(width=600, height=400) |
| 199 | +notepad.run() |
0 commit comments