`
wtx358
  • 浏览: 26321 次
  • 性别: Icon_minigender_1
  • 来自: 广西
社区版块
存档分类
最新评论

CodeEditor(PyQt4)

阅读更多
#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from highlighter import MyHighlighter
from highlighter import HighlightingRule

class CodeEditor(QPlainTextEdit):
    def __init__(self, parent = None):
        QPlainTextEdit.__init__(self, parent)
        self.lineNumberArea = LineNumberArea(self)
        
        self.connect(self, SIGNAL('blockCountChanged(int)'), \
                     self.updateLineNumberAreaWidth)
        self.connect(self, SIGNAL('updateRequest(const QRect &, int)'), \
                     self.updateLineNumberArea)
        self.connect(self, SIGNAL('cursorPositionChanged()'), \
                     self.highlightCurrentLine)
        
        self.updateLineNumberAreaWidth(0)
        self.highlightCurrentLine()
    def highlightCurrentLine(self):
        extraSelections = QTextEdit.extraSelections(QTextEdit())
        if not self.isReadOnly():
            selection = QTextEdit.ExtraSelection()
            lineColor = QColor(Qt.yellow).lighter(160)
            selection.format.setBackground(lineColor)
            selection.format.setProperty(QTextFormat.FullWidthSelection, True)
            selection.cursor = self.textCursor()
            selection.cursor.clearSelection()
            extraSelections.append(selection)
        self.setExtraSelections(extraSelections)
    def lineNumberAreaWidth(self):
        digits = 1
        Max = max(1, self.blockCount())
        while (Max >= 10) :
            Max /= 10
            digits += 1
        space = 3 + self.fontMetrics().width('9') * digits 
        return space
    def updateLineNumberAreaWidth(self, newBlockCount):
        self.setViewportMargins(self.lineNumberAreaWidth(), 0, 0, 0)
    def updateLineNumberArea(self, rect, dy):
        if dy:
            self.lineNumberArea.scroll(0, dy)
        else:
            self.lineNumberArea.update(0, rect.y(), \
                                       self.lineNumberAreaWidth(), \
                                       rect.height())
        if (rect.contains(self.viewport().rect())):
            self.updateLineNumberAreaWidth(0)
    def resizeEvent(self, e):
        QPlainTextEdit.resizeEvent(self, e)
        cr = self.contentsRect()
        self.lineNumberArea.setGeometry(QRect(cr.left(), cr.top(), \
                                              self.lineNumberAreaWidth(), \
                                              cr.height()))
    def lineNumberAreaPaintEvent(self, event):
        painter = QPainter(self.lineNumberArea)
        rect = event.rect()
        painter.fillRect(rect, Qt.lightGray)
        block = self.firstVisibleBlock()
        blockNumber = block.blockNumber()
        top = int(self.blockBoundingGeometry(block).translated(\
            self.contentOffset()).top())
        bottom = top + int(self.blockBoundingRect(block).height())
        while (block.isValid() and top <= rect.bottom()):
            if (block.isVisible() and bottom >= rect.top()):
                number = QString.number(blockNumber + 1)
                painter.setPen(Qt.black)
                painter.drawText(0, top, self.lineNumberArea.width(), \
                                 self.fontMetrics().height(), \
                                 Qt.AlignRight, number)
            block = block.next()
            top = bottom
            bottom = top + int(self.blockBoundingRect(block).height())
            blockNumber += 1

class LineNumberArea(QWidget):
    def __init__(self, editor):
        QWidget.__init__(self, editor)
        self.codeEditor = editor
    def sizeHint(self):
        return QSize(codeEditor.lineNumberAreaWidth(), 0)
    def paintEvent(self, event):
        self.codeEditor.lineNumberAreaPaintEvent(event)

def main():
    app = QApplication(sys.argv)
    editor = CodeEditor()
    editor.setWindowTitle('Code Editor Example')
    editor.show()
    app.exec_()
    
if __name__ == '__main__':
    main()

分享到:
评论

相关推荐

    使用pyqt5开发的一个简易计算器

    **PyQt5简介** PyQt5是Python编程语言与Qt框架的接口,它允许开发者使用Python来编写跨平台的GUI应用程序。Qt是一个功能强大的、开源的C++库,提供了丰富的图形用户界面元素和网络、数据库等多方面的支持。PyQt5则...

    QCodeEditor:基于Light QPlainTextEdit的小部件,它提供行号栏和语法以及当前行高亮(PyQt)

    **QCodeEditor** 是一个基于 PyQt 的代码编辑器小部件,设计用于提供类似于专业文本编辑器的功能,如行号栏、语法高亮和当前行突出显示。PyQt 是 Python 的一个库,它允许开发者利用 Qt 框架来创建桌面应用。在本...

    Orbit Code Editor:简单的轻量级python代码编辑器-开源

    Orbit Code Editor 是一个轻量级的 Python 编辑器,设计简单。 Orbit 是用 python 和 PyQt5 构建的。 快速启动可帮助您随时立即编码,只需打开它即可开始编码。 您不需要在文件名中添加 .py 扩展名,编辑器会自动...

    pyqt5+eric6安装包和配置说明

    1. **代码提示与自动完成**:在“Settings” -&gt; “Editor” -&gt; “Code Completion”中,启用你需要的功能,如自动完成、参数提示等。 2. **语法高亮**:在“Syntax Highlighting”中,你可以选择不同的主题,或者...

    pyQode:PyQtPySide的源代码编辑器小部件-Source code editor

    未维护 关于 是PyQt / PySide的源代码编辑器小部件。 您可以将其视为的替代。 该存储库包含和pyQode项目的 。 文献资料 检阅以开始使用。...该工具可让您使用填充程序而不是PyQt4 / PyQt5 / PyQide来

    Beginning Python: From Novice to Professional 第二版

    * Learn by following along with ten interesting projects, including a P2P file–sharing application, chat client, video game, remote text editor, and more. Complete, downloadable code is provided for ...

    eric5-5.3.1

    the PyQt4 package gets updated. If this step is omitted, a bunch of strange errors will occur. Installation of translations Translations of the eric5 IDE are available as separate downloads. ...

    Python库 | pyqode.core-2.6.6-py2.py3-none-any.whl

    editor = CodeEdit() # 启动后端服务器 server.start() editor.show() app.exec_() ``` 这个简单的例子展示了如何在PyQt5环境中创建一个带有Python后端支持的代码编辑器。 总之,`pyqode.core`是一个强大的Python...

    Python有价值资源合集

    - **PyQt5**:基于Qt的Python绑定库,提供了丰富的UI组件和功能。 #### Hardware(硬件) - **Adafruit CircuitPython**:为微控制器设计的Python实现,支持多种硬件设备。 - **Raspberry Pi Libraries**:为...

Global site tag (gtag.js) - Google Analytics