A text area can be created in Python PyQt with the widget QPlainTextEdit.
A multiline text field is QPlainTextEdit. The name sounds odd for a
textarea, but it is the right widget for plain text. It is faster than
QTextEdit because it does not handle rich text.
QPlainTextEdit is a multiline text area in PyQt. To set the text, we use its method insertPlainText(). We can set its position and size using the methods move() and resize().
If you are new to PyQt, I recommend the course below.
Introduction
Before you can make a window or add a text area, you should verify that you have PyQt installed and working on your computer.
The example below creates a text area QPlainTextEdit using PyQt5. You can load thie widget from PyQt5.QtWidgets.
from PyQt5.QtWidgets import QPlainTextEdit |
You’ll also want QMainWindow and QWidget
from PyQt5.QtWidgets import QMainWindow, QWidget, QPlainTextEdit |
Then you can create a new text area widget like this:
self.b = QPlainTextEdit(self) |
You can add text into the text area programatically:
self.b.insertPlainText("You can write text here.\n") |
Set its position (horizontal,vertical) inside the window:
self.b.move(10,10) |
And turn it into the change (width,height)
self.b.resize(400,200) |
QPlainTextEdit example
We will create the usual QMainWindow to add the widget to. It is just for plain text, like notepad. To add new lines we add the \n character.
import sys |
Working with it
setPlainText('hello')fills the field.toPlainText()reads it back.insertPlainText('more')adds text where the cursor is.setReadOnly(True)turns it into a viewer.textChangedfires on every change, that is how you get live validation.appendPlainText()adds a line at the bottom, handy for a log window.setFont(QFont('monospace'))makes it look like a code editor.
Wrap long lines with setLineWrapMode(QPlainTextEdit.WidgetWidth), and
scroll to the bottom with the verticalScrollBar().setValue(...) after you
append a line.
If you want to try this yourself, the exercises on PyChallenge turn the same idea into a few short problems with instant feedback.

Thx!