QPlainTextEdit Example In PyQt5

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

 

 

 

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

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:

 

 

Methods:

Text is inserted into the PlainTextEdit by either of the following methods:

 

 

 

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.

 

 

 

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.

 

 

 

Placeholder text can be placed into the PlainTextEdit with.

 

 

 

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:

 

 

 

 

Run your code and you will see this result

QPlainTextEdit Example In PyQt5
QPlainTextEdit Example In PyQt5

 

 

 

Also you can check more Python GUI articles in the below links

 

 

 

This is another example on PyQt5 QPainTextEdit.

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.

QPlainTextEdit Example In PyQt5
QPlainTextEdit Example In PyQt5

 

 

 

Text Editor with Toolbar

Now let’s create a simple TextEditor with QPlainTextEdi and PyQt5.

 

This is the example

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

PyQt5 TextEditor
PyQt5 TextEditor

 

 

 

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.

 

 

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.

 

 

 

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”

Leave a Comment

Share via
Copy link
Powered by Social Snap
×