南阳手机网站制作,建设网站工作室,wordpress双语版,工作室需要营业执照吗QScintilla 安装示例代码参考链接 安装 最近发现了一个有趣的库#xff0c;qt的插件库#xff0c;之前一直以为显示代码时是重写QTextEdit来实现的#xff0c;结果qt有现成的一个库来显示这些东西#xff0c;在此记录一下 # 安装 QScintilla
pip install QScintilla示例代码… QScintilla 安装示例代码参考链接 安装 最近发现了一个有趣的库qt的插件库之前一直以为显示代码时是重写QTextEdit来实现的结果qt有现成的一个库来显示这些东西在此记录一下 # 安装 QScintilla
pip install QScintilla示例代码
import sysfrom PyQt5.Qsci import QsciLexerPython, QsciScintilla
from PyQt5.Qt import *class MyQsciLexerPython(QsciLexerPython):def __init__(self, parentNone):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, parentNone):super().__init__(parent)# 1.设置文档的编码格式为 “utf8” 换行符为 windows 【可选linuxMac】self.setUtf8(True)self.setEolMode(QsciScintilla.SC_EOL_CRLF) # 文件中的每一行都以EOL字符结尾换行符为 \r \n# 2.设置括号匹配模式self.setBraceMatching(QsciScintilla.StrictBraceMatch) ## 3.设置 Tab 键功能self.setIndentationsUseTabs(True) # 行首缩进采用Tab键反向缩进是Shift Tabself.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 设置代码折叠后 的颜色FFFFFFself.setMarkerBackgroundColor(QColor(#FFBCBC), QsciScintilla.SC_MARKNUM_FOLDEREND)self.setMarkerForegroundColor(QColor(red), QsciScintilla.SC_MARKNUM_FOLDEREND)class Window(QWidget):def __init__(self, parentNone):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, encodingutf-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入门指南之编辑器设置