
- PyQt - Home
- PyQt - Introduction
- PyQt - Environment
- PyQt - Hello World
- PyQt - Major Classes
- PyQt - Using Qt Designer
- PyQt - Meta Objects
- PyQt Signals & Slots
- PyQt - Signals and Slots
- PyQt - Support and Signals
- PyQt - Unbound and Bound Signals
- PyQt - New Signals with PyQtSignal
- PyQt - Connecting, Disconnecting, & Emitting Signals
- PyQt - Slot decorator
- PyQt - Slot Connection
- PyQt Layouts
- PyQt - Layout Management
- PyQt - QBoxLayout
- PyQt - QGridLayout
- PyQt - QFormLayout
- PyQt - QHBoxLayout
- PyQt - QVBoxLayout
- PyQt - QStackedLayout
- PyQt - QGraphicsGridLayout
- PyQt - QGraphicsAnchorLayout
- PyQt - QGraphicsLayout
- PyQt - QGraphicsLinearLayout
- PyQt Basic Widgets
- PyQt - Basic Widgets
- PyQt - Qlabel Widget
- PyQt - QLineEdit Widget
- PyQt - QPushButton Widget
- PyQt - QRadioButton Widget
- PyQt - QCheckBox Widget
- PyQt - QComboBox Widget
- PyQt - QSpinBox Widget
- PyQt - QMessageBox
- PyQt - QDialogButtonBox Widget
- PyQt - QFontComboBox Widget
- PyQt - QDoubleSpinBox Widget
- PyQt - QToolBox Widget
- PyQt - QMenuBar, QMenu & Qaction Widgets
- PyQt - QToolTip
- PyQt - QInputDialog Widget
- PyQt - QFontDialog Widget
- PyQt - QDialog Widget
- PyQt - QFileDialog Widget
- PyQt - QTab Widget
- PyQt - QSplitter Widget
- PyQt - QDock Widget
- PyQt - QStatusBar Widget
- PyQt - QTabBar
- PyQt - QList Widget
- PyQt - QScrollBar Widget
- PyQt - QProgressBar
- PyQt - QCalendar Widget
- PyQt - QMessageBox Widget
- PyQt - QPlainTextEdit
- PyQt - QDateEdit
- PyQt - QDateTimeEdit
- PyQt - QTimeEdit
- PyQt - QTextEdit
- PyQt - QTextBrowser
- PyQt - QScrollArea
- PyQt - Drag and Drop
- PyQt - Multiple Document Interface
- PyQt - QDialog Class
- PyQt Views
- PyQt - QColumnView
- PyQt - QTableView
- PyQt Drawing API
- PyQt - Drawing API
- PyQt - Drawing a Line
- PyQt - Drawing a Rectangle
- PyQt - Drawing a Triangle
- PyQt - Drawing a Circle
- PyQt - Drawing a Ellipse
- PyQt - Drawing a Polygon
- PyQt - Geometric Transformation
- PyQt - Drawing Effect
- PyQt Groups
- PyQt - QButtonGroup
- PyQt - QGroupBox
- PyQt Effects
- PyQt - Effects
- PyQt - Opacity Effect
- PyQt - QGraphicsBlur Effect
- PyQt - QGraphicsColorize Effect
- PyQt - QGraphicsDropShadow Effect
- PyQt Events
- PyQt - Event Handling
- PyQt - File Open Event
- PyQt - Action Event
- PyQt - Hide Event
- PyQt - Resize Event
- PyQt Database
- PyQt - Database Handling
- PyQt Essentials
- PyQt - BrushStyle Constants
- PyQt - QClipboard
- PyQt - QPixmap Class
- PyQt Useful Resources
- PyQt - Quick Guide
- PyQt - Useful Resources
- PyQt - Discussion
PyQt - QTextEdit
In GUI development, building a text editor plays a crucial role for every computer user. While developing any software it always requires an editor to write the code or make documentation. In this tutorial, we learn the concept of QTextEdit to build a simple text editor in PyQt.
What is QTextEdit?
The QTextEdit is a class that can be used to display both plain and rich text.
QTextEdit is a text editing widget that works with paragraphs and characters. In its default setting, a new line represents a new paragraph. Each paragraph is a formatted string that is word-wrapped to fit into the widget's width. The document can have zero or more paragraphs, and each paragraph has its alignment. The hard line breaks separate paragraphs. Each character within a paragraph has unique attributes such as font and color.
We use QLabel to display the small set of rich text. In Qt framework, rich text are highly designed for online application tool and provide multi-featured web browser widget.
Note that the class QLineEdit provide users to build a widgets that support single line editing text but QTextEdit is specially designed for handling larger documentation.
Methods of QTextEdit class in PyQt
Below are the list of methods that are very useful to build a text editor using PyQt −
- QTextEdit() − This method reference from the class QTextEdit and create the text edit to the PyQt window.
- addWidget() − This method adds a widget at specified row and column
- setLayout() − This method is used to assign the layout object to the parent widget's layout.
- toPlainText() − This method is used to handle or optimize the large document and have quick response to user input.
- setPlainText() − This method is used to replace the existing text.
- setMinimumWidth() − It can set the minimum size of the window.
Example 1
Following example demonstrate the code snippet for simple GUI with two text edit widgets and a button. Here, the first widget allows users to enter any number and when clicking on the button widget it adds 20 to the existing number and generates the output in the second widget.
import sys from PyQt6.QtWidgets import QApplication, QWidget, QTextEdit, QVBoxLayout, QPushButton class TextEditDemo(QWidget): def __init__(self, parent=None): super().__init__(parent) # Setting the title of the GUI self.setWindowTitle("QTextEdit") # Setting the size of the window self.resize(300, 270) # Creating the first textedit self.textEdit1 = QTextEdit() # Creating the second textedit self.textEdit2 = QTextEdit() # Creating the button self.btnPress1 = QPushButton("Click Here") # Creating a vertical box layout layout = QVBoxLayout() # Adding the first textedit into the layout layout.addWidget(self.textEdit1) # Adding the second textedit into the layout layout.addWidget(self.textEdit2) # Adding the button into the layout layout.addWidget(self.btnPress1) self.setLayout(layout) # Calling the function when the button is clicked self.btnPress1.clicked.connect(self.btnPress1_Clicked) def btnPress1_Clicked(self): result = int(self.textEdit1.toPlainText()) + 20 # Displaying the result in the second textEdit. self.textEdit2.setPlainText(str(result)) if __name__ == '__main__': app = QApplication(sys.argv) win = TextEditDemo() win.show() sys.exit(app.exec())
Output
The above code produces the following output −

Example 2
Following example demonstrate the code snippet for text editor within a single widget container.
import sys from PyQt6.QtWidgets import QApplication, QWidget, QTextEdit, QFormLayout from PyQt6.QtCore import Qt class MainWindow(QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.setWindowTitle('QTexEdit') self.setMinimumWidth(200) layout = QFormLayout() self.setLayout(layout) text_edit = QTextEdit(self) layout.addRow(text_edit) self.show() if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() sys.exit(app.exec())
Output
The above code produces the following output−
