以下只是部分代码,需要完整代码点这里(不要积分哦):这里

最近一直在使用sublime text 3,发现sublime text相当好用,但是在windows上主题定制上还是不够完善,所以笔者也想开发一款类似的代码编辑器,拥有更加强大的主题定制功能。以下是我学习QScintilla时候做的笔记以及一个小demo,希望能够帮到想要学习QScintilla的同学。最后还有一个小问题没有解决:代码折叠以后QScintilla会有一条折叠线,找了很多资料都无法去除这条线,如果有解决的同学一定要告诉我解决方法哦!希望这个小demo能够帮到你!

最后使用环境是:python3.3.5和PyQt5.2.1

上一张效果图,有图有真相:

#!/usr/bin/python
#conding=utf-8#中文from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.Qsci import *
from util import ReadStyleSheet
import sysclass ScintillaEditor(QWidget):"""docstring for Scintilla"""scintilla = Nonefont = Nonefontmetrics = Nonedef __init__(self,parent=None):super(ScintillaEditor, self).__init__(parent)self.__init_window__()self.__init_ui__()self.__layout__()self.scintilla.textChanged.connect(self.textChanged)self.scintilla.linesChanged.connect(self.linesChanged)def linesChanged(self):print(self.scintilla.lines())self.scintilla.setMarginWidth(0, self.fontmetrics.width(str(self.scintilla.lines())) + 5)passdef textChanged(self):print("textChanged")passdef keyPressEvent(self,event):print("keyPressEvent")def __layout__(self):self.layout = QVBoxLayout()self.layout.setAlignment(Qt.AlignVCenter)self.layout.setContentsMargins(0,0,0,0)self.layout.setSpacing(0)self.layout.addWidget(self.label,False,Qt.AlignVCenter)self.layout.addWidget(self.scintilla)self.setLayout(self.layout)def __init_ui__(self):self.__init_scintilla__()self.__init_label__()def __init_label__(self):self.label = QLabel("Coder")self.label.setAlignment(Qt.AlignHCenter|Qt.AlignVCenter)self.label.setMinimumSize(0,30)def __init_window__(self):self.setWindowTitle("Coder")self.setMinimumSize(800,500)#set window no border#self.setWindowFlags(Qt.FramelessWindowHint)self.font = QFont()self.font.setFamily("Microsoft YaHei UI Light")self.font.setPointSize(10)self.font.setFixedPitch(True)self.setFont(self.font)self.fontmetrics = QFontMetrics(self.font)def __init_scintilla__(self):self.scintilla = QsciScintilla()self.scintilla.setUtf8(True)self.scintilla.setFont(self.font)self.scintilla.setMarginsFont(self.font)#set line number widthself.scintilla.setMarginWidth(0, self.fontmetrics.width(str(self.scintilla.lines())) + 5)self.scintilla.setMarginLineNumbers(0, True)#mesure lineself.scintilla.setEdgeMode(QsciScintilla.EdgeLine)self.scintilla.setEdgeColumn(150)self.scintilla.setEdgeColor(QColor("#BBB8B5"))#brace matchself.scintilla.setBraceMatching(QsciScintilla.StrictBraceMatch)#current line colorself.scintilla.setCaretLineVisible(True)self.scintilla.setCaretLineBackgroundColor(QColor("#2D2D2D"))self.scintilla.setCaretForegroundColor(QColor("white"))#selection colorself.scintilla.setSelectionBackgroundColor(QColor("#606060"))self.scintilla.setSelectionForegroundColor(QColor("#FFFFFF"))#table relativeself.scintilla.setIndentationsUseTabs(True)self.scintilla.setIndentationWidth(4)self.scintilla.setTabIndents(True)self.scintilla.setAutoIndent(True)self.scintilla.setBackspaceUnindents(True)self.scintilla.setTabWidth(4)#indentation guidesself.scintilla.setIndentationGuides(True)#line number margin colorself.scintilla.setMarginsBackgroundColor(QColor("#272727"))self.scintilla.setMarginsForegroundColor(QColor("#CCCCCC"))#folding marginself.scintilla.setFolding(QsciScintilla.PlainFoldStyle)self.scintilla.setMarginWidth(2,12)#markerself.scintilla.markerDefine(QsciScintilla.Minus,QsciScintilla.SC_MARKNUM_FOLDEROPEN)self.scintilla.markerDefine(QsciScintilla.Plus,QsciScintilla.SC_MARKNUM_FOLDER)self.scintilla.markerDefine(QsciScintilla.Minus,QsciScintilla.SC_MARKNUM_FOLDEROPENMID)self.scintilla.markerDefine(QsciScintilla.Plus,QsciScintilla.SC_MARKNUM_FOLDEREND)#marker define colorself.scintilla.setMarkerBackgroundColor(QColor("#FFFFFF"),QsciScintilla.SC_MARKNUM_FOLDEREND)self.scintilla.setMarkerForegroundColor(QColor("#272727"),QsciScintilla.SC_MARKNUM_FOLDEREND)self.scintilla.setMarkerBackgroundColor(QColor("#FFFFFF"),QsciScintilla.SC_MARKNUM_FOLDEROPENMID)self.scintilla.setMarkerForegroundColor(QColor("#272727"),QsciScintilla.SC_MARKNUM_FOLDEROPENMID)#self.scintilla.setMarkerBackgroundColor(QColor("#FFFFFF"),QsciScintilla.SC_MARKNUM_FOLDERMIDTAIL)#self.scintilla.setMarkerForegroundColor(QColor("#272727"),QsciScintilla.SC_MARKNUM_FOLDERMIDTAIL)#self.scintilla.setMarkerBackgroundColor(QColor("#FFFFFF"),QsciScintilla.SC_MARKNUM_FOLDERTAIL)#self.scintilla.setMarkerForegroundColor(QColor("#272727"),QsciScintilla.SC_MARKNUM_FOLDERTAIL)self.scintilla.setMarkerBackgroundColor(QColor("#FFFFFF"),QsciScintilla.SC_MARKNUM_FOLDERSUB)self.scintilla.setMarkerForegroundColor(QColor("#272727"),QsciScintilla.SC_MARKNUM_FOLDERSUB)self.scintilla.setMarkerBackgroundColor(QColor("#FFFFFF"),QsciScintilla.SC_MARKNUM_FOLDER)self.scintilla.setMarkerForegroundColor(QColor("#272727"),QsciScintilla.SC_MARKNUM_FOLDER)self.scintilla.setMarkerBackgroundColor(QColor("#FFFFFF"),QsciScintilla.SC_MARKNUM_FOLDEROPEN)self.scintilla.setMarkerForegroundColor(QColor("#272727"),QsciScintilla.SC_MARKNUM_FOLDEROPEN)self.scintilla.setFoldMarginColors(QColor("#272727"),QColor("#272727"))#whitespaceself.scintilla.setWhitespaceVisibility(QsciScintilla.WsInvisible)self.scintilla.setWhitespaceSize(2)"""the default margin is:0: line number,width is not zero1: width is zero2: folding, width is not zero"""self.scintilla.setMarginWidth(1,0)#set lexerself.lexer = QsciLexerPython()self.lexer.setFont(self.font)self.lexer.setColor(QColor("#ffffff"))self.scintilla.setLexer(self.lexer)#high light codeself.lexer.setColor(QColor("#ffffff"))self.lexer.setPaper(QColor("#333333"))self.lexer.setColor(QColor("#5BA5F7"),QsciLexerPython.ClassName)self.lexer.setColor(QColor("#FF0B66"),QsciLexerPython.Keyword)self.lexer.setColor(QColor("#00FF40"),QsciLexerPython.Comment)self.lexer.setColor(QColor("#BD4FE8"),QsciLexerPython.Number)self.lexer.setColor(QColor("#F1E607"),QsciLexerPython.DoubleQuotedString)self.lexer.setColor(QColor("#F1E607"),QsciLexerPython.TripleSingleQuotedString)self.lexer.setColor(QColor("#F1E607"),QsciLexerPython.TripleDoubleQuotedString)self.lexer.setColor(QColor("#F1E607"),QsciLexerPython.DoubleQuotedString)self.lexer.setColor(QColor("#04F452"),QsciLexerPython.FunctionMethodName)self.lexer.setColor(QColor("#FFFFFF"),QsciLexerPython.Operator)self.lexer.setColor(QColor("#FFFFFF"),QsciLexerPython.Identifier)self.lexer.setColor(QColor("#F1E607"),QsciLexerPython.CommentBlock)self.lexer.setColor(QColor("#F1E607"),QsciLexerPython.UnclosedString)self.lexer.setColor(QColor("#F1E607"),QsciLexerPython.HighlightedIdentifier)self.lexer.setColor(QColor("#F1E607"),QsciLexerPython.Decorator)self.scintilla.setText(open("Editor.py",encoding="UTF8").read())self.scintilla.setMarginWidth(0, self.fontmetrics.width(str(self.scintilla.lines())) + 5)if __name__ == "__main__":app = QApplication(sys.argv)window = ScintillaEditor()scrollbar = ReadStyleSheet.ReadStyleSheetFile("stylesheet/scrollbar.css")scintilla = ReadStyleSheet.ReadStyleSheetFile("stylesheet/scintilla.css")common = ReadStyleSheet.ReadStyleSheetFile("stylesheet/common.css")stylesheet = scrollbar + scintilla + commonwindow.setStyleSheet(stylesheet)window.show()app.exec_()sys.exit()       

QScintilla相关推荐

  1. QScintilla的信号汇总和解释

    入门 可以先看我这个博客,知道基本知识: QScintilla学习大全_我是标同学的博客-CSDN博客_qscintilla 根据类的继承关系,我们能直接使用的信号在 QsciScintillaBas ...

  2. 关于编辑器QScintilla(Scintilla)词法分析器取消非活动代码灰色显示

    入门,首先看我这两篇博客:关于QScintilla库的入门大全https://biao2488890051.blog.csdn.net/article/details/126798996?spm=10 ...

  3. QScintilla的各种颜色字体等设置

    入门的话,先看我这篇博客:QScintilla学习大全_我是标同学的博客-CSDN博客_qscintilla 仅仅当作普通编辑器 这个编辑器QsciScintilla类,自己有 字体.背景色设置函数, ...

  4. 关于编辑器QScintilla(Scintilla)词法分析器工作原理的分析(实现注释区分)

    入门,首先看我这两篇博客:关于QScintilla库的入门大全https://biao2488890051.blog.csdn.net/article/details/126798996?spm=10 ...

  5. PyQt4 Python GUI窗体应用程序

    目录 目录 前言 软件环境 PyQT简介 Setup PyCharm Setup SIP Setup PyQt4 测试PyQt是否安装成功 常见错误 最后 前言 还是一句老话,公司要什么我就做什么.这 ...

  6. Python的库和资源

    2019独角兽企业重金招聘Python工程师标准>>> 由于设计者和开源社区的共同努力,在python中有大量优秀的库可以被直接调用以高效地完成不同需求的工作.这里列举一些常见常用的 ...

  7. python开发的软件sparrow-黑客常用wifi蓝牙分析攻击工具,让你的设备陷入危险之中...

    工具概述 Sparrow-wifi本质上一款针对下一代2.4GHz和5GHz的WiFi频谱感知工具,它不仅提供了GUI图形化用户界面,而且功能更加全面,可以代替类似inSSIDer和linssid之类 ...

  8. python语言入门m-Python2 教程

    Python是什么? Python(英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种面向对象的解释型计算机程序设计语言,由荷兰人Guido van Rossum于1989年发明 ...

  9. QT官方第三方开源工具

    QT官方第三方开源工具.今天再次在网上看到了QT的第三方开源工具列表,这里留下link地址,以备将来用到的时候参考使用. http://qt-project.org/wiki/Category:Add ...

最新文章

  1. 如何解决Connect超时导致的阻塞问题
  2. mysql 引起服务器死机_MSSQL数据库占用内存过大造成服务器死机问题的解决方法...
  3. Git安装步骤+Mac终端配置
  4. Linux打印介绍【转贴】
  5. 物联网技术或颠覆传统高等教育
  6. jboss java路径_JBOSS常用配置文件的路径 - liangy的个人空间 - 51Testing软件测试网 51Testing软件测试网-软件测试人的精神家园...
  7. 关于Web网页设计规范简述
  8. 程序员技能与成长:程序员的必备工具箱(值得收藏)
  9. 数学对于编程有多重要?
  10. 广西南宁机器人比赛_缤纷校园|2018年广西中小学电脑机器人竞赛开赛 南宁学子大显身手...
  11. 修行漫谈——说说中年危机
  12. android intent开启前置摄像头
  13. F5 LTM 常用oid列表
  14. Flutter集成个推推送-安卓原生篇
  15. [C#] UI跨执行绪
  16. 源代码VS业务的悟道——知行合一
  17. Manifest Permissions
  18. linux R语言 安装
  19. Java程序员高效学习的六个中肯建议
  20. 本量利分析计算机,2017年自学考试(管理会计(一))知识点复习:成本性态分析和变动成本法--本量利分析的应用[经营杠杆在利润预测中的应用]...

热门文章

  1. meta常用标签总结
  2. 数据结构和算法-001 数组
  3. 【莓闻】2009年黑莓增长显著 智能手机领域第一
  4. Java范例集锦(一)
  5. BZOJ 4679/Hdu5331 Simple Problem LCT or 树链剖分
  6. bzoj 1037: [ZJOI2008]生日聚会Party
  7. Error:Execution failed for task ':myapp:dexDebug'. com.android.ide.common.process.ProcessExcepti
  8. [转]改变UITextField placeHolder颜色、字体
  9. 服务端发post请求产生的编码问题
  10. Android中的“再按一次返回键退出程序”实现