Modification of the colour of the words in QPlainTextEdit
-
I have an object:
self.textEdit = QPlainTextEdit()
in code:dark_theme = ''' QWidget{ background-color: #dedede; } QPlainTextEdit{ background-color: #282c34; font: 700 16pt \"Zen Kurenaido\"; padding: 8px; color: #aeb4b4; border-radius: 10px; }'''
class Ui_application_pages(object):
def setupUi(self, application_pages):
if not application_pages.objectName():
application_pages.setObjectName(u"application_pages")
application_pages.resize(1056, 657)
self.page_1 = QWidget()
self.page_1.setObjectName(u"page_1")
self.page_1.setStyleSheet(dark_theme)
self.page_1.setStyleSheet('background-color: #22252c')
self.centrallayout = QHBoxLayout(self.page_1)
self.centrallayout.setObjectName(u"centrallayout")self.textEdit = QPlainTextEdit() self.textEdit.setStyleSheet(dark_theme) self.textEdit.setObjectName(u"lineEdit")
I want the word on my text editor.
print
Anyway, that's the word that changed its color, and the rest of the words in the line and the editor were not.
-
You can try just like the example below.
Look, I didn't exactly set an example and interval between the words of one gap.
index = index + len(word) + 1
If you need something more serious, check the question. https://ru.stackoverflow.com/questions/901830
import sys from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.Qt import *
dark_theme = '''
QWidget{
background-color: #dedede;
}
QPlainTextEdit{
background-color: #282c34;
font: 700 16pt "Zen Kurenaido";
padding: 8px;
color: #aeb4b4;
border-radius: 10px;
}
'''class Ui_application_pages(object):
def setupUi(self, application_pages):
if not application_pages.objectName():
application_pages.setObjectName(u"application_pages")
application_pages.resize(1056, 657)self.page_1 = QWidget() self.page_1.setObjectName(u"page_1") self.page_1.setStyleSheet(dark_theme) self.page_1.setStyleSheet('background-color: #22252c') self.centrallayout = QHBoxLayout(self.page_1) self.centrallayout.setObjectName(u"centrallayout") self.textEdit = QPlainTextEdit() self.textEdit.setStyleSheet(dark_theme) self.textEdit.setObjectName(u"lineEdit")
class Window(QtWidgets.QWidget, Ui_application_pages):
def init(self, text):
super().init()
self.setupUi(self)self.layout = QVBoxLayout(self) self.layout.addWidget(self.page_1) self.textEdit.setPlainText(text) self.centrallayout.addWidget(self.textEdit) self.format = QTextCharFormat() self.format.setTextOutline(QPen(QColor("green"))) self.timer = QtCore.QTimer() self.timer.timeout.connect(self.updateTime) self.timer.start(500) def updateTime(self): wordList = self.textEdit.toPlainText().split() index = 0 for word in wordList: cursor = self.textEdit.textCursor() cursor.setPosition(index) ww = cursor.movePosition(QTextCursor.EndOfWord, 1) self.format.setTextOutline(QPen(QColor("#aeb4b4"))) cursor.mergeCharFormat(self.format) if word in ['print', 'print,', 'print.', 'print(']: self.format.setTextOutline(QPen(QColor("green"))) cursor.mergeCharFormat(self.format) else: self.format.setTextOutline(QPen(QColor("#aeb4b4"))) cursor.mergeCharFormat(self.format) index = index + len(word) + 1
text = '''Функция print
Функция print Python выводит заданные объекты на стандартное устройство
вывода (экран) или отправляет их текстовым потоком в файл.
Полный синтаксис функции print():
print( *items, sep=' ', end='\n', file=sys.stdout, flush=False ).
Параметр file контролирует то, куда выводятся значения функции print.
По умолчанию все выводится на стандартный поток вывода - sys.stdout.
Обратите внимание на то, что все аргументы, которые управляют поведением
функции print, надо передаватьprint как ключевые, а не позиционные.'''if name == 'main':
app = QtWidgets.QApplication(sys.argv)
w = Window(text)
w.show()
sys.exit(app.exec_())