如何改变空白字符的颜色?
How to change color of whitespace characters?
我正在使用 Qt 和 Python(PySide2) 制作一个文本编辑器。像大多数其他文本编辑器一样,我想让 whitespaces(space, tab, new line...) 可见并成功。 QTextEdit 和 QTextOption 将白色 space 字符显示为可见的特殊字符。 (space 到点,制表符到箭头,换行到反向-P)
但那些特殊字符的颜色与其他字符相同。我认为如果颜色不同,它会更具可读性。
如何改变白色spaces特殊字符的颜色?
orgText = """
\t\tAll those moments
\t\twill be lost
in time
like tears
in rain.
It's time to die."""
option = QtGui.QTextOption()
option.setFlags(QtGui.QTextOption.ShowTabsAndSpaces | QtGui.QTextOption.ShowLineAndParagraphSeparators)
self.teOrg.setPlainText(orgText)
self.teOrg.document().setDefaultTextOption(option)
如果你想根据文本给出一个特殊的,那么你应该使用 QSyntaxHighlighter。为此,您必须获取模式的开始和结束,并使用 setFormat()
更改格式。
我只能修改 space (" "
) 和制表符 ("\t"
) 的格式,但不能修改段落分隔符 (¶
):
import re
from PySide2 import QtCore, QtGui, QtWidgets
orgText = """
\t\tAll those moments
\t\twill be lost
in time
like tears
in rain.
It's time to die."""
class Highlighter(QtGui.QSyntaxHighlighter):
def __init__(self, parent=None):
super(Highlighter, self).__init__(parent)
space_format = QtGui.QTextCharFormat()
space_format.setBackground(QtGui.QColor("salmon"))
tab_format = QtGui.QTextCharFormat()
tab_format.setBackground(QtGui.QColor("lightgray"))
self.highlightingRules = [
(r"( )*", space_format),
(r"(\t)*", tab_format),
]
def highlightBlock(self, text):
for pattern, fmt in self.highlightingRules:
expression = re.compile(pattern)
m = expression.search(text)
while m is not None:
start, end = m.span()
self.setFormat(start, end - start, fmt)
m = expression.search(text, end + 1)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.teOrg = QtWidgets.QTextEdit()
self.setCentralWidget(self.teOrg)
self.teOrg.setPlainText(orgText)
option = QtGui.QTextOption()
option.setFlags(
QtGui.QTextOption.ShowTabsAndSpaces
| QtGui.QTextOption.ShowLineAndParagraphSeparators
)
self.teOrg.document().setDefaultTextOption(option)
self.highlighter = Highlighter(self.teOrg.document())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(320, 240)
w.show()
sys.exit(app.exec_())
最后,我重写了@eyllanesc 的代码。
import sys
import typing
import re
from PySide2 import QtCore
from PySide2 import QtGui
from PySide2 import QtWidgets
class SyntaxHighlighter(QtGui.QSyntaxHighlighter):
def __init__(self, parent:typing.Union[QtCore.QObject, QtGui.QTextDocument, None]=None):
super().__init__(parent)
self.spaceFmt = QtGui.QTextCharFormat()
self.spaceFmt.setForeground(QtGui.QColor('red'))
self.expression = re.compile(r'\s+', re.U | re.S | re.M)
def highlightBlock(self, text:str):
for match in self.expression.finditer(text):
start, end = match.span()
self.setFormat(start, end - start, self.spaceFmt)
class TextEditWin(QtWidgets.QMainWindow):
def __init__(self):
# Initialize ui.
super().__init__()
self.resize(800, 600)
self.textEdit = QtWidgets.QTextEdit(self)
self.setCentralWidget(self.textEdit)
self.textEdit.setFontPointSize(15)
# Make space characters visible.
option = QtGui.QTextOption()
option.setFlags(QtGui.QTextOption.ShowTabsAndSpaces | QtGui.QTextOption.ShowLineAndParagraphSeparators)
self.textEdit.document().setDefaultTextOption(option)
# Change color of space characters.
self.highlighter = SyntaxHighlighter(self.textEdit.document())
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
win = TextEditWin()
win.show()
sys.exit(app.exec_())
我的代码也不改变段落分隔符的颜色(¶)。我认为这是因为 Qt 在调用 highlightBlock() 时传递了除 '\n' 之外的文本。
谢谢@eyllanesc。
我正在使用 Qt 和 Python(PySide2) 制作一个文本编辑器。像大多数其他文本编辑器一样,我想让 whitespaces(space, tab, new line...) 可见并成功。 QTextEdit 和 QTextOption 将白色 space 字符显示为可见的特殊字符。 (space 到点,制表符到箭头,换行到反向-P)
但那些特殊字符的颜色与其他字符相同。我认为如果颜色不同,它会更具可读性。
如何改变白色spaces特殊字符的颜色?
orgText = """
\t\tAll those moments
\t\twill be lost
in time
like tears
in rain.
It's time to die."""
option = QtGui.QTextOption()
option.setFlags(QtGui.QTextOption.ShowTabsAndSpaces | QtGui.QTextOption.ShowLineAndParagraphSeparators)
self.teOrg.setPlainText(orgText)
self.teOrg.document().setDefaultTextOption(option)
如果你想根据文本给出一个特殊的,那么你应该使用 QSyntaxHighlighter。为此,您必须获取模式的开始和结束,并使用 setFormat()
更改格式。
我只能修改 space (" "
) 和制表符 ("\t"
) 的格式,但不能修改段落分隔符 (¶
):
import re
from PySide2 import QtCore, QtGui, QtWidgets
orgText = """
\t\tAll those moments
\t\twill be lost
in time
like tears
in rain.
It's time to die."""
class Highlighter(QtGui.QSyntaxHighlighter):
def __init__(self, parent=None):
super(Highlighter, self).__init__(parent)
space_format = QtGui.QTextCharFormat()
space_format.setBackground(QtGui.QColor("salmon"))
tab_format = QtGui.QTextCharFormat()
tab_format.setBackground(QtGui.QColor("lightgray"))
self.highlightingRules = [
(r"( )*", space_format),
(r"(\t)*", tab_format),
]
def highlightBlock(self, text):
for pattern, fmt in self.highlightingRules:
expression = re.compile(pattern)
m = expression.search(text)
while m is not None:
start, end = m.span()
self.setFormat(start, end - start, fmt)
m = expression.search(text, end + 1)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.teOrg = QtWidgets.QTextEdit()
self.setCentralWidget(self.teOrg)
self.teOrg.setPlainText(orgText)
option = QtGui.QTextOption()
option.setFlags(
QtGui.QTextOption.ShowTabsAndSpaces
| QtGui.QTextOption.ShowLineAndParagraphSeparators
)
self.teOrg.document().setDefaultTextOption(option)
self.highlighter = Highlighter(self.teOrg.document())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(320, 240)
w.show()
sys.exit(app.exec_())
最后,我重写了@eyllanesc 的代码。
import sys
import typing
import re
from PySide2 import QtCore
from PySide2 import QtGui
from PySide2 import QtWidgets
class SyntaxHighlighter(QtGui.QSyntaxHighlighter):
def __init__(self, parent:typing.Union[QtCore.QObject, QtGui.QTextDocument, None]=None):
super().__init__(parent)
self.spaceFmt = QtGui.QTextCharFormat()
self.spaceFmt.setForeground(QtGui.QColor('red'))
self.expression = re.compile(r'\s+', re.U | re.S | re.M)
def highlightBlock(self, text:str):
for match in self.expression.finditer(text):
start, end = match.span()
self.setFormat(start, end - start, self.spaceFmt)
class TextEditWin(QtWidgets.QMainWindow):
def __init__(self):
# Initialize ui.
super().__init__()
self.resize(800, 600)
self.textEdit = QtWidgets.QTextEdit(self)
self.setCentralWidget(self.textEdit)
self.textEdit.setFontPointSize(15)
# Make space characters visible.
option = QtGui.QTextOption()
option.setFlags(QtGui.QTextOption.ShowTabsAndSpaces | QtGui.QTextOption.ShowLineAndParagraphSeparators)
self.textEdit.document().setDefaultTextOption(option)
# Change color of space characters.
self.highlighter = SyntaxHighlighter(self.textEdit.document())
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
win = TextEditWin()
win.show()
sys.exit(app.exec_())
我的代码也不改变段落分隔符的颜色(¶)。我认为这是因为 Qt 在调用 highlightBlock() 时传递了除 '\n' 之外的文本。
谢谢@eyllanesc。