![pyqt5 text edit pyqt5 text edit](https://linuxhint.com/wp-content/uploads/2021/02/image2-11.png)
PYQT5 TEXT EDIT CODE
The preceding code shows the complete TextEdit subclass.
![pyqt5 text edit pyqt5 text edit](https://coderslegacy.com/wp-content/uploads/2020/07/PyQt5_QTextEdit.jpg)
If event.type() = QEvent.KeyPress and \ event.key() = Qt.Key_Tab: cursor = self.textCursor() cursor.insertText(" ") return True return QTextEdit.event(self, event) We also set the QTextDocument's modified flag to False. We get the file handle, create a QTextStream, set the encoding to UTF-8, and output the entire text using QTextEdit.toPlainText(). The code for saving (not shown) is very similar to that for loading. The file-handling code is similar to what we have seen before the only real difference is that we set the QTextDocument's modified flag to False. If a file is loaded-for example, by the user invoking the File^Open action-this method is called. QMessageBox.warning(self, "Python Editor - Load Error", "Failed to load %s: %s" % (self.filename, e)) Raise IOError, unicode(fh.errorString()) stream = QTextStream(fh) tCodec("UTF-8") (stream.readAll()) ().setModified(False) tWindowTitle("Python Editor - %s" % \ QFileInfo(self.filename).fileName()) except (IOError, OSError), e: The updateUi() method is used to enable and disable actions depending on the application's state see the Enabling and Disabling Actions sidebar on page 385.
![pyqt5 text edit pyqt5 text edit](https://doc.qt.io/qtforpython/_images/textfinder-example-find.png)
The result is a blank QTextEdit with no unsaved changes. This method simply retrieves and clears the QTextDocument that actually holds the text, and sets the modified flag to False. If not self.okToContinue(): return document = () document.clear() tModified(False) self.filename = None tWindowTitle("Python Editor - Unnamed") self.updateUi() The only other methods we will look at are two of the three basic file-handling ones, since they involve the text editor. The rest of the initializer is concerned with the creation of the actions, menus, and toolbars, things that we are very familiar with and can therefore skip. We make the editor the main window's central widget. Next we create a PythonHighlighter, a QSyntaxHighlighter subclass, passing it the text editor's QTextDocument-this is the object in which the editor's text and formatting are actually stored. Then we create a TextEdit, a custom QTextEdit class that differs from QTextEdit in that it converts Tab key presses into insertions of four spaces. We begin by creating a fixed-pitch font (a font where every character is the same width). Self.highlighter = PythonHighlighter(()) tCentralWidget(self.editor) Figure 13.1 The Python Editor editing itself Since we have covered the creation of such applications before, we will focus just on those parts that are relevant to rich text editing, starting with the beginning of the main window's initializer.ĭef_init_(self, filename=None, parent=None):įont = QFont("Courier", 11) tFixedPitch(True) self.editor = TextEdit() (font) As the screenshot in Figure 13.1 shows, the Python Editor is a simple main-window-style application with two menus and toolbars.
PYQT5 TEXT EDIT HOW TO
However, no off-the-shelf editor will necessarily work in just the way we want, and since creating our own is instructive and revealing of what is involved, we will create a simple Python Editor to learn how to use QTextEdit and QSyntaxHighlighter. And for more power, we can use Eric4, itself written in PyQt and using QScintilla for its text editing. The Tkinter-based IDLE application provides both a "sandbox" in which we can experiment with Python code, and a perfectly good Python code editor. If we want a Python-savvy text editor with syntax highlighting, we need not create one ourselves.