This article covers the PyQt5 QTextEdit widget.
Like the QLineEdit widget, QTextEdit is used to take input from the user in the form of text. However, unlike QLineEdit which only takes input in a single line, QTextEdit offers a large area where the User can input several lines of text, or even several paragraphs.
You will find a list of useful methods for the QTextEdit available at the end of this page.
Creating a QTextEdit Widget
If you’ve been following our previous articles, you’ll have noticed that to create the window, we used to use QMainWindow. Here however we are using QWidget. There isn’t much of a difference, but if you want the TextEdit to work properly, use QWidget.
Other than this, to create a simple QTextEdit widget, just use the QTextEdit Class to create an object.
from PyQt5 import QtWidgets from PyQt5.QtWidgets import QApplication, QWidget import sys app = QApplication(sys.argv) window = QWidget() window.setGeometry(400,400,300,300) window.setWindowTitle("CodersLegacy") date = QtWidgets.QTextEdit(window) window.show() sys.exit(app.exec_())
The GUI output of the above code:
Retrieving Text from QTextEdit
Now it’s time to get the entered text from the user. Using the
toPlainText() method and the QPushButton widget, we can accomplish our goal.
from PyQt5 import QtWidgets from PyQt5.QtWidgets import QApplication, QWidget import sys def display(): print(text_edit.toPlainText()) app = QApplication(sys.argv) window = QWidget() window.setGeometry(400,400,300,300) window.setWindowTitle("CodersLegacy") text_edit = QtWidgets.QTextEdit(window) text_edit.setPlaceholderText("Enter some text here") button = QtWidgets.QPushButton(window) button.setText("Press me") button.clicked.connect(display) button.move(100,200) window.show() sys.exit(app.exec_())
A short video showing the usage of the PyQt5 QTextEdit widget.
You can find an explanation for the
setPlaceholderText() method down below in the methods table.
move() is a very basic layout system, and not suitable for actual projects. PyQt5 comes with actual layouts, in charge of managing positions of the widgets inside the window. Learn more about them in our PyQt5 layout management tutorial.
A compilation of all the important methods for the QTextEdit widget.
|Returns the displayed text on the TextEdit widget.|
|Used to set the displayed text on the widget.|
|Used to set whether the widget is going to be read-only (cannot be written to) or not.|
|Returns a Boolean value depending on whether the TextEdit is read only or not.|
|Used to set some grayed out text into the widget that disappears when the widget is interacted with.|
|Disables/Enables the ability for the user to use the Undo and Redo features.|
|Returns a True or False value depending on whether Undo/Redo is enabled.|
We went through the dozens of different methods in the documentation and picked the ones we thought might be the most useful.
A link back to the PyQt5 Home page.
This marks the end of the PyQt5 QTextEdit article. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.