PyQt5 QPlainTextEdit widget

Category: Computers

Example of a QPlainTextEdit widget using Python 3 and PyQt5.


  • Create QPlainTextEdit widget
  • Change its appearance
  • Connect signal to take action when text changes
  • Get text out of the widget
  • Put text into the widget
import sys

from PyQt5.QtWidgets import QApplication, QMainWindow, QPlainTextEdit

if __name__ == '__main__':
    app = QApplication(sys.argv)

    main_window = QMainWindow()

    # Create text entry box
    text_edit_widget = QPlainTextEdit()

    # Change font, colour of text entry box
        """QPlainTextEdit {background-color: #333;
                           color: #00FF00;
                           text-decoration: underline;
                           font-family: Courier;}""")

    # "Central Widget" expands to fill all available space

    # Print text to console whenever it changes
        lambda: print(text_edit_widget.document().toPlainText()))

    # Set initial value of text
    text_edit_widget.document().setPlainText("Type text in here")

    # Start event loop