qt代码显示,包含文本颜色设置等

安装

最近发现了一个有趣的库,qt的插件库,之前一直以为显示代码时是重写QTextEdit来实现的,结果qt有现成的一个库来显示这些东西,在此记录一下

# 安装 QScintilla
pip install QScintilla

示例代码

import sys

from PyQt5.Qsci import QsciLexerPython, QsciScintilla
from PyQt5.Qt import *


class MyQsciLexerPython(QsciLexerPython):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setColor(QColor(0, 0, 0))  # 设置默认的字体颜色
        self.setPaper(QColor(255, 255, 255))  # 设置底色
        self.setColor(QColor("#B0171F"), QsciLexerPython.Keyword)
        self.setColor(QColor("#008000"), QsciLexerPython.Comment)  # 块注释 的颜色
        self.setColor(QColor("#007f7f"), QsciLexerPython.Number)  # 数字 的颜色
        self.setColor(QColor("#ff00ff"), QsciLexerPython.DoubleQuotedString)  # 双引号字符串的颜色
        self.setColor(QColor("#ff00ff"), QsciLexerPython.SingleQuotedString)  # 单引号字符的颜色
        self.setColor(QColor("#191970"), QsciLexerPython.Operator)
        self.setColor(QColor("#000000"), QsciLexerPython.Identifier)  # 可识别字符的颜色,这个范围很广,包含了关键词,函数名;所以要取消这句
        self.setColor(QColor("#0000FF"), QsciLexerPython.UnclosedString)  # 未完成输入的字符串的颜色


class MyQsciScintilla(QsciScintilla):
    def __init__(self, parent=None):
        super().__init__(parent)
        # 1.设置文档的编码格式为 “utf8” ,换行符为 windows   【可选linux,Mac】
        self.setUtf8(True)
        self.setEolMode(QsciScintilla.SC_EOL_CRLF)  # 文件中的每一行都以EOL字符结尾(换行符为 \r \n)
        # 2.设置括号匹配模式
        self.setBraceMatching(QsciScintilla.StrictBraceMatch)  #
        # 3.设置 Tab 键功能
        self.setIndentationsUseTabs(True)  # 行首缩进采用Tab键,反向缩进是Shift +Tab
        self.setIndentationWidth(4)  # 行首缩进宽度为4个空格
        self.setIndentationGuides(True)  # 显示虚线垂直线的方式来指示缩进
        self.setTabIndents(True)  # 编辑器将行首第一个非空格字符推送到下一个缩进级别
        self.setAutoIndent(True)  # 插入新行时,自动缩进将光标推送到与前一个相同的缩进级别
        self.setBackspaceUnindents(True)
        self.setTabWidth(4)  # Tab 等于 4 个空格
        # 4.设置光标
        self.setCaretWidth(2)  # 光标宽度(以像素为单位),0表示不显示光标
        self.setCaretForegroundColor(QColor("darkCyan"))  # 光标颜色
        self.setCaretLineVisible(True)  # 是否高亮显示光标所在行
        self.setCaretLineBackgroundColor(QColor('#FFCFCF'))  # 光标所在行的底色
        # 5.设置页边特性。        这里有3种Margin:[0]行号    [1]改动标识   [2]代码折叠
        # 5.1 设置行号
        font = QFont()
        # font.setFamilies(['Segoe UI', 'Microsoft YaHei', 'PingFang SC'])
        font.setFamily('JetBrains Mono')
        font.setPixelSize(14)
        self.setFont(font)
        # self.setMarginsFont(font)  # 行号字体
        self.setMarginLineNumbers(0, True)  # 设置标号为0的页边显示行号
        self.setMarginWidth(0, 40)  # 行号宽度
        self.setMarkerForegroundColor(QColor("#FFFFFF"), 0)
        # 5.2 设置改动标记
        self.setMarginType(1, QsciScintilla.SymbolMargin)  # 设置标号为1的页边用于显示改动标记
        self.setMarginWidth(1, "0000")  # 改动标记占用的宽度
        # img = QPixmap(":/leftside.png")  # 改动标记图标,大小是48 x 48
        # sym_1 = img.scaled(QSize(16, 16))  # 图标缩小为 16 x 16
        # self.markerDefine(sym_1, 0)
        # self.setMarginMarkerMask(1, 0b1111)
        self.setMarkerForegroundColor(QColor("#ee1111"), 1)  # 00ff00
        # 5.3  设置代码自动折叠区域
        self.setFolding(QsciScintilla.PlainFoldStyle)
        self.setMarginWidth(2, 12)
        # 5.3.1 设置代码折叠和展开时的页边标记 - +
        self.markerDefine(QsciScintilla.Minus, QsciScintilla.SC_MARKNUM_FOLDEROPEN)
        self.markerDefine(QsciScintilla.Plus, QsciScintilla.SC_MARKNUM_FOLDER)
        self.markerDefine(QsciScintilla.Minus, QsciScintilla.SC_MARKNUM_FOLDEROPENMID)
        self.markerDefine(QsciScintilla.Plus, QsciScintilla.SC_MARKNUM_FOLDEREND)
        # 5.3.2 设置代码折叠后,+ 的颜色FFFFFF
        self.setMarkerBackgroundColor(QColor("#FFBCBC"), QsciScintilla.SC_MARKNUM_FOLDEREND)
        self.setMarkerForegroundColor(QColor("red"), QsciScintilla.SC_MARKNUM_FOLDEREND)


class Window(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.vBox = QVBoxLayout(self)
        self.editor = MyQsciScintilla(self)
        self.lexer = MyQsciLexerPython(self.editor)
        self.editor.setLexer(self.lexer)
        self.vBox.addWidget(self.editor)

        self.setFileText('MCSL2.py')

    def setFileText(self, fileName: str):
        with open(fileName, 'r', encoding='utf-8') as f:
            self.editor.setText(f.read())


if __name__ == '__main__':
    QApplication.setHighDpiScaleFactorRoundingPolicy(
        Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)

    app = QApplication(sys.argv)
    app.setQuitOnLastWindowClosed(True)
    demo = Window()
    demo.resize(800, 600)
    demo.show()
    sys.exit(app.exec_())

参考链接

PYQT5:基于QsciScintilla的代码编辑器分析5–多文档编辑区介绍
QScintilla入门指南之编辑器设置

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值