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. 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. 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. Use setText() to display the word number on the second label.If we want a Python-savvy text editor with syntax highlighting, we need not create one ourselves. Len (text.split()) is the number of words in text. Split() converts words in a string into a list. Text_changed method is called, the text in the text editor (self.te) is stored in the text variable using the toPlainText() method. Using a vertical box layout, place two labels and one text editor in a vertical direction. Whenever text in a text editor is modified, text_changed method is called. The label below displays the word number. If setAcceptRichText is false, it is all recognized as plain text. I created a text editor using the QTextEdit() class. Related course: Create Desktop Apps with Python PyQt5ĭescription self.lbl1 = QLabel('Enter your sentence:') When you type text in a text editor, display the number of words below. ('The number of words is ' + str(len(text.split()))) Self.lbl2 = QLabel('The number of words is 0') Self.lbl1 = QLabel('Enter your sentence:') import sysįrom PyQt5.QtWidgets import QApplication, QWidget, QLabel, QTextEdit, QVBoxLayout Let's create a simple program that displays word numbers using two labels and one text editor. ![]() The QTextEdit class provides an editor for editing and displaying both plain text and rich text.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |