In this post i want to show you How To Create QTextEdit In PyQt5 . so QTextEdit is an advanced WYSIWYG viewer/editor supporting rich text formatting using HTML-style tags, or Markdown format. It is optimized to handle large documents and to respond quickly to user input. QTextEdit works on paragraphs and characters. A paragraph is a formatted string which is word-wrapped to fit into the width of the widget. By default when reading plain text, one newline signifies a paragraph. we are going to use some codes from the previous articles.
Read More
How To Create QTextEdit In PyQt5
Now in the first we need some imports.
1 2 3 4 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QTextEdit import sys from PyQt5.QtGui import QIcon |
After we create our main Window class that extends from QMainWindow and in that class we add some requirements of the window also we are going to create a method of CreateMenu() , in that method we create our menus and toolbars. these are the codes that we have described in the previous articles. now we are just focusing on creating QTextEdit.
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 |
def CreateMenu(self): mainMenu = self.menuBar() fileMenu = mainMenu.addMenu('File') editMenu = mainMenu.addMenu('Edit') viewMenu = mainMenu.addMenu('View') helpMenu = mainMenu.addMenu('Help') copyAction = QAction(QIcon("copy.png"), 'Copy', self) copyAction.setShortcut("Ctrl+C") fileMenu.addAction(copyAction) saveAction = QAction(QIcon("Save.png"), 'Save', self) saveAction.setShortcut("Ctrl+S") fileMenu.addAction(saveAction) pasteAction = QAction(QIcon("Paste.png"), 'Paste', self) pasteAction.setShortcut("Ctrl+P") fileMenu.addAction(pasteAction) exiteAction = QAction(QIcon("exit.png"), 'Exit', self) exiteAction.setShortcut("Ctrl+E") exiteAction.triggered.connect(self.exitWindow) fileMenu.addAction(exiteAction) self.toolbar = self.addToolBar('Toolbar') self.toolbar.addAction(copyAction) self.toolbar.addAction(saveAction) self.toolbar.addAction(pasteAction) self.toolbar.addAction(exiteAction) def exitWindow(self): self.close() |
Now we create our createEditor() method and in that method we write our QTextEdit codes
1 2 3 |
def createEditor(self): self.textEdit = QTextEdit(self) self.setCentralWidget(self.textEdit) |
Also every PyQt5 application must create an application object. The sys.argv
parameter is a list of arguments from a command line.
1 |
App = QApplication(sys.argv) |
Finally, we enter the mainloop of the application. The event handling starts from this point.
1 2 |
window = Window() sys.exit(App.exec_()) |
Run the complete code and this will be the result
Complete source code for How To Create QTextEdit In PyQt5
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 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QTextEdit import sys from PyQt5.QtGui import QIcon class Window(QMainWindow): def __init__(self): super().__init__() # Window properties self.title = "Codeloop - PyQt5 TextEdit" self.top = 200 self.left = 500 self.width = 680 self.height = 480 # Set window icon and title self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) # Create text editor self.createEditor() # Create menu and toolbar self.CreateMenu() # Display the window self.show() def CreateMenu(self): # Create menu bar and menus mainMenu = self.menuBar() fileMenu = mainMenu.addMenu('File') editMenu = mainMenu.addMenu('Edit') viewMenu = mainMenu.addMenu('View') helpMenu = mainMenu.addMenu('Help') # Define actions and connect signals copyAction = QAction(QIcon("copy.png"), 'Copy', self) copyAction.setShortcut("Ctrl+C") fileMenu.addAction(copyAction) saveAction = QAction(QIcon("Save.png"), 'Save', self) saveAction.setShortcut("Ctrl+S") fileMenu.addAction(saveAction) pasteAction = QAction(QIcon("Paste.png"), 'Paste', self) pasteAction.setShortcut("Ctrl+P") fileMenu.addAction(pasteAction) exitAction = QAction(QIcon("exit.png"), 'Exit', self) exitAction.setShortcut("Ctrl+E") exitAction.triggered.connect(self.exitWindow) fileMenu.addAction(exitAction) # Create toolbar and add actions self.toolbar = self.addToolBar('Toolbar') self.toolbar.addAction(copyAction) self.toolbar.addAction(saveAction) self.toolbar.addAction(pasteAction) self.toolbar.addAction(exitAction) def exitWindow(self): # Close the application self.close() def createEditor(self): # Create a QTextEdit widget as central widget self.textEdit = QTextEdit(self) self.setCentralWidget(self.textEdit) # Create QApplication instance and run the application App = QApplication(sys.argv) window = Window() sys.exit(App.exec_()) |
This PyQt5 code creates a simple text editor application with a menu bar and a toolbar. The main window contains a QTextEdit widget where users can type and edit text.
The menu bar includes standard options like File, Edit, View, and Help, each with corresponding actions such as Copy, Save, Paste, and Exit. These actions are also added to the toolbar for easy access.
This program uses PyQt5 QMainWindow class to create the main window and arrange the components. QIcon is used to set icons for menu actions.
So we can say that this code provides a basic template for building text editor applications with PyQt5, and it demonstrates how to create menus, toolbar, and connect actions to functionality such as copying, saving, pasting, and exiting the application.
Subscribe and Get Free Video Courses & Articles in your Email