In this PyQt5 post i want to show you creating QPlainTextEdit Example In PyQt5, The PlainTextEdit widget is optimized to display plain text content.If the application is to display formatted text, the TextEdit widget should be used.
What is PyQt5 ?
PyQt5 is Python binding for the Qt cross platform application framework. it allows developers to create desktop applications with graphical user interface (GUI) using the Python programming language. PyQt5 is developed by Riverbank Computing and is licensed under the GPL and commercial licenses.
Qt is popular framework for developing graphical user interfaces, and PyQt5 provides access to all of Qt’s functionality, including support for widgets, layouts, graphics, multimedia and networking. it also provides Pythonic API for working with Qt, which makes it easy to create and manage GUIs in Python.
To install PyQt5, you can use pip
1 |
pip install PyQt5 |
What is QPlainTextEdit in PyQt5 ?
QPlainTextEdit is a widget class in the PyQt5 library that provides plain text editor widget that can be used to edit and display plain text. It is a multiline text box that allows users to enter and edit text in simple format, without any formatting such as bold or italic.
QPlainTextEdit widget provides different features for working with text including basic editing functionality like copy, cut, paste, undo and redo, as well as the ability to set text color, background color, and font. It also provides features such as syntax highlighting, line numbering, and word wrapping.
QPlainTextEdit can be used in different types of applications such as text editors, log viewers, and console applications, where a simple, plain text editor is needed. It can be customized with signals and slots to perform various tasks such as updating the display of text in real-time or processing the input text.
How to Create TextEdit in PyQt5?
This is the complete code for QPlainTextEdit Example 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 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QWidget, QPlainTextEdit, QVBoxLayout import sys class Window(QWidget): def __init__(self): super().__init__() # Window properties self.title = "PyQt5 Plain TextEdit" self.top = 200 self.left = 500 self.width = 400 self.height = 300 # Initialize window components self.InitWindow() def InitWindow(self): # Set window icon and properties self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) # Create a vertical layout vbox = QVBoxLayout() # Create a PlainTextEdit widget plainText = QPlainTextEdit() # Set placeholder text for the PlainTextEdit plainText.setPlaceholderText("This is some text for our plaintextedit") # Set initial text content text = "Welcome to Codeloop.org" plainText.appendPlainText(text) # Disable undo and redo functionality plainText.setUndoRedoEnabled(False) # Add PlainTextEdit widget to the layout vbox.addWidget(plainText) # Set the layout for the window self.setLayout(vbox) # Display the window self.show() # Create the application instance App = QApplication(sys.argv) # Create the window instance window = Window() # Execute the application sys.exit(App.exec_()) |
in the above code first we have imported some important classes that we are using for creating QPlainTextEdit, after that we have created our Window class that extends from QWidget. we have created our InitWindow() method, in that method we have created a QVBoxLayout. and also we have added a QPlainTextEdit in our window.
Constructor:
The PlainTextEdit widget is constructed by using:
1 |
plaintextedit = QPlainTextEdit() |
Methods:
Text is inserted into the PlainTextEdit by either of the following methods:
1 2 |
plaintextedit.appendPlainText(text) plaintextedit.insertPlainText(text) |
The appendPlainText() method adds the new text to the end of the current text block whereas the insertPlainText() method adds the text at the cursor position. By default, the text in the PlainTextEdit can be modified by the user. It can however be used as a read-only widget with.
1 |
plaintextedit.setReadOnly(read_only) |
When read_only is set to True, the user will only be able to navigate through the text.
The read-only state of the widget can also be retrieved using.
1 |
plaintextedit.isReadOnly() |
Placeholder text can be placed into the PlainTextEdit with.
1 |
plaintextedit.setPlaceholderText(text) |
The text specified will only be shown in the widget when there is no text loaded.
the title of the document can be set via:
1 |
plaintextedit.setDocumentTitle(title) |
Run your code and you will see this result
Also you can check more Python GUI articles in the below links
- Kivy GUI Development Tutorials
- TKinter GUI Development Tutorials
- Psyide2 GUI Development
- wxPython GUI Development Tutorials
- PyQt5 GUI Development Tutorials
This is another example on PyQt5 QPainTextEdit.
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 |
import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QPlainTextEdit, QAction, QFileDialog class MainWindow(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(100, 100, 500, 500) self.setWindowTitle('QPlainTextEdit Example') self.text_edit = QPlainTextEdit(self) self.setCentralWidget(self.text_edit) self.init_toolbar() self.show() def init_toolbar(self): # create actions new_action = QAction('New', self) new_action.setShortcut('Ctrl+N') new_action.triggered.connect(self.new_file) open_action = QAction('Open', self) open_action.setShortcut('Ctrl+O') open_action.triggered.connect(self.open_file) save_action = QAction('Save', self) save_action.setShortcut('Ctrl+S') save_action.triggered.connect(self.save_file) # create toolbar toolbar = self.addToolBar('Main Toolbar') toolbar.addAction(new_action) toolbar.addAction(open_action) toolbar.addAction(save_action) def new_file(self): self.text_edit.clear() def open_file(self): filename = QFileDialog.getOpenFileName(self, 'Open file', '', 'Text Files (*.txt)') if filename[0]: with open(filename[0], 'r') as f: self.text_edit.setPlainText(f.read()) def save_file(self): filename = QFileDialog.getSaveFileName(self, 'Save file', '', 'Text Files (*.txt)') if filename[0]: with open(filename[0], 'w') as f: f.write(self.text_edit.toPlainText()) if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() sys.exit(app.exec_()) |
This code creates simple text editor window that contains QPlainTextEdit widget, main toolbar with three actions (New, Open, and Save), and event handlers for the toolbar actions.
initUI() method creates main window and sets the geometry and title. It creates QPlainTextEdit widget and sets it as the central widget of the main window. it also calls init_toolbar() method to create the toolbar and add actions to it.
init_toolbar() method creates three actions for the toolbar (New, Open, and Save), and adds them to the toolbar using addAction() method.
new_file(), open_file(), and save_file() methods are event handlers for the toolbar actions. they are called when the user clicks the corresponding toolbar action. new_file() clears text in the QPlainTextEdit widget, open_file() opens file dialog to select file to open and loads the file into the text editor, and save_file() saves the text in the editor to a file selected using a file dialog.
and also this code creates QApplication instance, creates MainWindow instance, and starts the event loop using app.exec_(). when the event loop is started, the main window is displayed and the user can interact with it.
Run your complete code and this is the result.
Text Editor with Toolbar
Now let’s create a simple TextEditor with QPlainTextEdi and PyQt5.
This is the example
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 |
import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QPlainTextEdit, QAction, QFileDialog class TextEditorWithToolbar(QMainWindow): def __init__(self): super().__init__() # Set window geometry and title self.setGeometry(100, 100, 500, 500) self.setWindowTitle('QPlainTextEdit Example') # Create QPlainTextEdit widget and set it as the central widget self.text_edit = QPlainTextEdit(self) self.setCentralWidget(self.text_edit) # Initialize the toolbar self.init_toolbar() # Show the main window self.show() def init_toolbar(self): # Create actions for New, Open, and Save new_action = QAction('New', self) new_action.setShortcut('Ctrl+N') new_action.triggered.connect(self.new_file) open_action = QAction('Open', self) open_action.setShortcut('Ctrl+O') open_action.triggered.connect(self.open_file) save_action = QAction('Save', self) save_action.setShortcut('Ctrl+S') save_action.triggered.connect(self.save_file) # Add actions to the main toolbar toolbar = self.addToolBar('Main Toolbar') toolbar.addAction(new_action) toolbar.addAction(open_action) toolbar.addAction(save_action) def new_file(self): # Clear the text edit widget self.text_edit.clear() def open_file(self): # Open file dialog to select a file to open filename = QFileDialog.getOpenFileName(self, 'Open file', '', 'Text Files (*.txt)') if filename[0]: # Read the content of the selected file with open(filename[0], 'r') as f: self.text_edit.setPlainText(f.read()) def save_file(self): # Open file dialog to select a file to save filename = QFileDialog.getSaveFileName(self, 'Save file', '', 'Text Files (*.txt)') if filename[0]: # Write the content of the text edit widget to the selected file with open(filename[0], 'w') as f: f.write(self.text_edit.toPlainText()) if __name__ == '__main__': # Create the application instance and main window app = QApplication(sys.argv) window = TextEditorWithToolbar() # Start the event loop sys.exit(app.exec_()) |
This code creates a simple text editor window with a QPlainTextEdit widget and a main toolbar containing New, Open, and Save actions. Users can create new files, open existing files, and save the contents using the toolbar.
Run the code and this will be the result
FAQs:
What is the difference between QPlainTextEdit and QTextEdit?
- QPlainTextEdit: It is a plain text editor widget that is suitable for editing and displaying plain text. It does not support rich text formatting, such as bold, italic or colors. QPlainTextEdit is typically used for editing source code, log files or any other plain text documents.
- QTextEdit: It is a rich text editor widget that supports formatted text, including styles, fonts, colors, and images. QTextEdit allows users to create and edit rich text documents with different formatting options. It is commonly used for creating documents, emails, or any text content that requires rich formatting.
How do I get text from QTextEdit?
For geting the text from a QTextEdit widget, you can use toPlainText() method. This method returns the plain text content of the QTextEdit widget as a string.
1 |
text = text_edit.toPlainText() |
How do I add text to QTextEdit?
For adding text to a QTextEdit widget, you can use append() method to append text to the end of the existing content, or you can use the insertPlainText() method to insert text at a specific position.
1 2 3 4 5 |
# Append text to the end text_edit.append("New text") # Insert text at a specific position text_edit.insertPlainText("Inserted text") |
How to display text in PyQt?
For displaying text in a PyQt application, you can use various widgets depending on your requirements:
- QLabel: Use QLabel to display static text or images.
- QTextEdit: Use QTextEdit to display and edit formatted text.
- QPlainTextEdit: Use QPlainTextEdit to display and edit plain text.
- QTextBrowser: Use QTextBrowser for read-only display of rich text documents.
- QListView: Use QListView to display text or items in a list format.
Subscribe and Get Free Video Courses & Articles in your Email
1 thought on “QPlainTextEdit Example In PyQt5”