一、()也显示红色
from PySide2.QtCore import Qt, QRegExp
from PySide2.QtGui import QColor, QTextCharFormat, QFont, QSyntaxHighlighter
from PySide2.QtWidgets import QApplication, QMainWindow, QPlainTextEdit
from PySide2.QtCore import Qt, QRegExp
from PySide2.QtGui import QColor, QTextCharFormat, QFont, QSyntaxHighlighter
class PythonHighlighter(QSyntaxHighlighter):
def __init__(self, parent=None):
super().__init__(parent)
keywordFormat = QTextCharFormat()
keywordFormat.setForeground(Qt.darkBlue)
keywordFormat.setFontWeight(QFont.Bold)
keywords = ['def', 'class', 'if', 'else', 'elif', 'for', 'while', 'try', 'except', 'finally', 'raise', 'return']
self.highlightingRules = [(QRegExp('\\b' + keyword + '\\b'), keywordFormat) for keyword in keywords]
self.defFormat = QTextCharFormat()
self.defFormat.setForeground(Qt.red)
self.defFormat.setFontWeight(QFont.Bold)
self.defPattern = QRegExp("\\bdef\\b")
self.leftParenthesisFormat = QTextCharFormat()
self.leftParenthesisFormat.setForeground(Qt.red)
self.leftParenthesisPattern = QRegExp("\\(")
self.rightParenthesisFormat = QTextCharFormat()
self.rightParenthesisFormat.setForeground(Qt.red)
self.rightParenthesisPattern = QRegExp("\\)")
def highlightBlock(self, text):
for pattern, format in self.highlightingRules:
expression = QRegExp(pattern)
index = expression.indexIn(text)
while index >= 0:
length = expression.matchedLength()
self.setFormat(index, length, format)
index = expression.indexIn(text, index + length)
self.setCurrentBlockState(0)
defStartIndex = 0
if self.previousBlockState() != 1:
defStartIndex = self.defPattern.indexIn(text)
while defStartIndex >= 0:
defEndIndex = text.index('(', defStartIndex)
if defEndIndex == -1:
self.setCurrentBlockState(1)
defEndIndex = len(text)
else:
self.setFormat(defEndIndex, 1, self.leftParenthesisFormat)
self.setFormat(defStartIndex, defEndIndex - defStartIndex + 1, self.defFormat)
defStartIndex = self.defPattern.indexIn(text, defEndIndex)
leftParenthesisIndex = self.leftParenthesisPattern.indexIn(text)
while leftParenthesisIndex >= 0:
rightParenthesisIndex = self.rightParenthesisPattern.indexIn(text, leftParenthesisIndex)
if rightParenthesisIndex == -1:
self.setCurrentBlockState(2)
rightParenthesisIndex = len(text)
else:
self.setFormat(rightParenthesisIndex, 1, self.rightParenthesisFormat)
self.setFormat(leftParenthesisIndex, 1, self.leftParenthesisFormat)
leftParenthesisIndex = self.leftParenthesisPattern.indexIn(text, rightParenthesisIndex)
class CodeEditor(QPlainTextEdit):
def __init__(self, parent=None):
super().__init__(parent)
font = QFont()
font.setFamily('Courier')
font.setFixedPitch(True)
font.setPointSize(10)
self.setFont(font)
highlighter = PythonHighlighter(self.document())
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.editor = CodeEditor()
self.setCentralWidget(self.editor)
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
二、()不显示红色
def highlightBlock(self, text):
self.setCurrentBlockState(0)
defStartIndex = 0
if self.previousBlockState() != 1:
defStartIndex = self.defPattern.indexIn(text)
while defStartIndex >= 0:
defEndIndex = -1
if '(' in text:
defEndIndex = text.index('(', defStartIndex)
if defEndIndex == -1:
self.setCurrentBlockState(1)
defEndIndex = len(text)
self.setFormat(defStartIndex, defEndIndex - defStartIndex, self.defFormat)
defStartIndex = self.defPattern.indexIn(text, defEndIndex)
for pattern, format in self.highlightingRules:
expression = QRegExp(pattern)
index = expression.indexIn(text)
while index >= 0:
length = expression.matchedLength()
self.setFormat(index, length, format)
index = expression.indexIn(text, index + length)