yside2 demo:制作一个py语言的代码编辑器,特殊字符蓝色高亮、定义类和方法时为红色高亮

from PySide2.QtCore import Qt, QRegExp
from PySide2.QtGui import QColor, QTextCharFormat, QFont, QSyntaxHighlighter
from PySide2.QtWidgets import QApplication, QMainWindow, QPlainTextEdit


class PythonHighlighter(QSyntaxHighlighter):
    def __init__(self, parent=None):
        super().__init__(parent)

        keyword_format = QTextCharFormat()
        keyword_format.setForeground(Qt.red)
        keyword_format.setFontWeight(QFont.Bold)

        keyword_patterns = [
            "\\bFalse\\b", "\\bNone\\b", "\\bTrue\\b",
            "\\band\\b", "\\bas\\b", "\\bassert\\b",
            "\\bbreak\\b", "\\bclass\\b", "\\bcontinue\\b",
            "\\bdef\\b", "\\bdel\\b", "\\belif\\b", "\\belse\\b",
            "\\bexcept\\b", "\\bfinally\\b", "\\bfor\\b",
            "\\bfrom\\b", "\\bglobal\\b", "\\bif\\b",
            "\\bimport\\b", "\\bin\\b", "\\bis\\b",
            "\\blambda\\b", "\\bnonlocal\\b", "\\bnot\\b",
            "\\bor\\b", "\\bpass\\b", "\\braise\\b",
            "\\breturn\\b", "\\btry\\b", "\\bwhile\\b",
            "\\bwith\\b", "\\byield\\b"
        ]

        self.highlighting_rules = [(QRegExp(pattern), keyword_format)
                                   for pattern in keyword_patterns]

        class_format = QTextCharFormat()
        class_format.setFontWeight(QFont.Bold)
        class_format.setForeground(Qt.darkMagenta)
        self.highlighting_rules.append((QRegExp("\\bQ[A-Za-z]+\\b"),
                                        class_format))

        quotation_format = QTextCharFormat()
        quotation_format.setForeground(Qt.darkGreen)
        self.highlighting_rules.append((QRegExp("\".*\""), quotation_format))
        self.highlighting_rules.append((QRegExp("\'.*\'"), quotation_format))

        function_format = QTextCharFormat()
        function_format.setFontItalic(True)
        function_format.setForeground(Qt.blue)
        self.highlighting_rules.append((QRegExp("\\b[A-Za-z0-9_]+(?=\\()"),
                                        function_format))

    def highlightBlock(self, text):
        for pattern, format in self.highlighting_rules:
            expression = QRegExp(pattern)
            index = expression.indexIn(text)
            while index >= 0:
                length = expression.matchedLength()
                self.setFormat(index, length, format)
                index = expression.indexIn(text, index + length)


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_()

它包含了一些正则表达式和格式化规则,用于高亮Python代码中的关键字、类、函数和字符串。然后,我们创建了一个名为CodeEditor的自定义QPlainTextEdit类,它使用PythonHighlighter来高亮文本。最后,我们创建了一个名为MainWindow的主窗口,并将CodeEditor设置为其中央部件。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值