Welcome folks today in this post we will be creating a advanced notepad
which supports crud
operations and also various other advanced features in pyqt5
library which is useful creating gui
applications in python. All the full source code of the application will be given below.
Get Started
In order to get started you need to install the following library by pip
command as shown below
pip install pyqt5
After installing this library make an app.py
file and copy paste the following code to it
app.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 |
# importing required libraries from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtPrintSupport import * import os import sys # Creating main window class class MainWindow(QMainWindow): # constructor def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) # setting window geometry self.setGeometry(100, 100, 600, 400) # creating a layout layout = QVBoxLayout() # creating a QPlainTextEdit object self.editor = QPlainTextEdit() # setting font to the editor fixedfont = QFontDatabase.systemFont(QFontDatabase.FixedFont) fixedfont.setPointSize(12) self.editor.setFont(fixedfont) # self.path holds the path of the currently open file. # If none, we haven't got a file open yet (or creating new). self.path = None # adding editor to the layout layout.addWidget(self.editor) # creating a QWidget layout container = QWidget() # setting layout to the container container.setLayout(layout) # making container as central widget self.setCentralWidget(container) # creating a status bar object self.status = QStatusBar() # setting stats bar to the winow self.setStatusBar(self.status) # creating a file tool bar file_toolbar = QToolBar("File") # adding file tool bar to the window self.addToolBar(file_toolbar) # creating a file menu file_menu = self.menuBar().addMenu("&File") # creating actions to add in the file menu # creating a open file action open_file_action = QAction("Open file", self) # setting status tip open_file_action.setStatusTip("Open file") # adding action to the open file open_file_action.triggered.connect(self.file_open) # adding this to file menu file_menu.addAction(open_file_action) # adding this to tool bar file_toolbar.addAction(open_file_action) # similarly creating a save action save_file_action = QAction("Save", self) save_file_action.setStatusTip("Save current page") save_file_action.triggered.connect(self.file_save) file_menu.addAction(save_file_action) file_toolbar.addAction(save_file_action) # similarly creating save action saveas_file_action = QAction("Save As", self) saveas_file_action.setStatusTip("Save current page to specified file") saveas_file_action.triggered.connect(self.file_saveas) file_menu.addAction(saveas_file_action) file_toolbar.addAction(saveas_file_action) # for print action print_action = QAction("Print", self) print_action.setStatusTip("Print current page") print_action.triggered.connect(self.file_print) file_menu.addAction(print_action) file_toolbar.addAction(print_action) # creating another tool bar for editing text edit_toolbar = QToolBar("Edit") # adding this tool bar to the main window self.addToolBar(edit_toolbar) # creating a edit menu bar edit_menu = self.menuBar().addMenu("&Edit") # adding actions to the tool bar and menu bar # undo action undo_action = QAction("Undo", self) # adding status tip undo_action.setStatusTip("Undo last change") # when triggered undo the editor undo_action.triggered.connect(self.editor.undo) # adding this to tool and menu bar edit_toolbar.addAction(undo_action) edit_menu.addAction(undo_action) # redo action redo_action = QAction("Redo", self) redo_action.setStatusTip("Redo last change") # when triggered redo the editor redo_action.triggered.connect(self.editor.redo) # adding this to menu and tool bar edit_toolbar.addAction(redo_action) edit_menu.addAction(redo_action) # cut action cut_action = QAction("Cut", self) cut_action.setStatusTip("Cut selected text") # when triggered cut the editor text cut_action.triggered.connect(self.editor.cut) # adding this to menu and tool bar edit_toolbar.addAction(cut_action) edit_menu.addAction(cut_action) # copy action copy_action = QAction("Copy", self) copy_action.setStatusTip("Copy selected text") # when triggered copy the editor text copy_action.triggered.connect(self.editor.copy) # adding this to menu and tool bar edit_toolbar.addAction(copy_action) edit_menu.addAction(copy_action) # paste action paste_action = QAction("Paste", self) paste_action.setStatusTip("Paste from clipboard") # when triggered paste the copied text paste_action.triggered.connect(self.editor.paste) # adding this to menu and tool bar edit_toolbar.addAction(paste_action) edit_menu.addAction(paste_action) # select all action select_action = QAction("Select all", self) select_action.setStatusTip("Select all text") # when this triggered select the whole text select_action.triggered.connect(self.editor.selectAll) # adding this to menu and tool bar edit_toolbar.addAction(select_action) edit_menu.addAction(select_action) # wrap action wrap_action = QAction("Wrap text to window", self) wrap_action.setStatusTip("Check to wrap text to window") # making it checkable wrap_action.setCheckable(True) # making it checked wrap_action.setChecked(True) # adding action wrap_action.triggered.connect(self.edit_toggle_wrap) # adding it to edit menu not to the tool bar edit_menu.addAction(wrap_action) # calling update title methpd self.update_title() # showing all the components self.show() # creating dialog critical method # to show errors def dialog_critical(self, s): # creating a QMessageBox object dlg = QMessageBox(self) # setting text to the dlg dlg.setText(s) # setting icon to it dlg.setIcon(QMessageBox.Critical) # showing it dlg.show() # action called by file open action def file_open(self): # getting path and bool value path, _ = QFileDialog.getOpenFileName(self, "Open file", "", "Text documents (*.txt);All files (*.*)") # if path is true if path: # try opening path try: with open(path, 'rU') as f: # read the file text = f.read() # if some error occured except Exception as e: # show error using critical method self.dialog_critical(str(e)) # else else: # update path value self.path = path # update the text self.editor.setPlainText(text) # update the title self.update_title() # action called by file save action def file_save(self): # if there is no save path if self.path is None: # call save as method return self.file_saveas() # else call save to path method self._save_to_path(self.path) # action called by save as action def file_saveas(self): # opening path path, _ = QFileDialog.getSaveFileName(self, "Save file", "", "Text documents (*.txt);All files (*.*)") # if dialog is cancelled i.e no path is selected if not path: # return this method # i.e no action performed return # else call save to path method self._save_to_path(path) # save to path method def _save_to_path(self, path): # get the text text = self.editor.toPlainText() # try catch block try: # opening file to write with open(path, 'w') as f: # write text in the file f.write(text) # if error occurs except Exception as e: # show error using critical self.dialog_critical(str(e)) # else do this else: # change path self.path = path # update the title self.update_title() # action called by print def file_print(self): # creating a QPrintDialog dlg = QPrintDialog() # if executed if dlg.exec_(): # print the text self.editor.print_(dlg.printer()) # update title method def update_title(self): # setting window title with prefix as file name # suffix aas PyQt5 Notepad self.setWindowTitle("%s - PyQt5 Notepad" %(os.path.basename(self.path) if self.path else "Untitled")) # action called by edit toggle def edit_toggle_wrap(self): # chaning line wrap mode self.editor.setLineWrapMode(1 if self.editor.lineWrapMode() == 0 else 0 ) # drivers code if __name__ == '__main__': # creating PyQt5 application app = QApplication(sys.argv) # setting application name app.setApplicationName("PyQt5-Note") # creating a main window object window = MainWindow() # loop app.exec_() |
Now if you run this python script
by running the below command as shown below
python app.py