K.M*_*ier 8 python pyqt python-3.x qscintilla pyqt5
我正在尝试使用 PyQt5 中的基本语法突出显示、代码完成和可点击的函数和变量来制作一个简单的文本编辑器。我最大的希望是使用 PyQt5 的 QScintilla 端口
。
我在 Eli Bendersky 网站 ( http://eli.thegreenplace.net/2011/04/01/sample-using-qscintilla-with-pyqt )上找到了以下基于 QScintilla 的文本编辑器示例,Victor S. 已将其改编为 PyQt5 )。我认为这个例子是一个很好的起点:
#-------------------------------------------------------------------------
# qsci_simple_pythoneditor.pyw
#
# QScintilla sample with PyQt
#
# Eli Bendersky (eliben@gmail.com)
# This code is in the public domain
#-------------------------------------------------------------------------
import sys
import sip
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.Qsci import QsciScintilla, QsciLexerPython
class SimplePythonEditor(QsciScintilla):
ARROW_MARKER_NUM = 8
def __init__(self, parent=None):
super(SimplePythonEditor, self).__init__(parent)
# Set the default font
font = QFont()
font.setFamily('Courier')
font.setFixedPitch(True)
font.setPointSize(10)
self.setFont(font)
self.setMarginsFont(font)
# Margin 0 is used for line numbers
fontmetrics = QFontMetrics(font)
self.setMarginsFont(font)
self.setMarginWidth(0, fontmetrics.width("00000") + 6)
self.setMarginLineNumbers(0, True)
self.setMarginsBackgroundColor(QColor("#cccccc"))
# Clickable margin 1 for showing markers
self.setMarginSensitivity(1, True)
# self.connect(self,
# SIGNAL('marginClicked(int, int, Qt::KeyboardModifiers)'),
# self.on_margin_clicked)
self.markerDefine(QsciScintilla.RightArrow,
self.ARROW_MARKER_NUM)
self.setMarkerBackgroundColor(QColor("#ee1111"),
self.ARROW_MARKER_NUM)
# Brace matching: enable for a brace immediately before or after
# the current position
#
self.setBraceMatching(QsciScintilla.SloppyBraceMatch)
# Current line visible with special background color
self.setCaretLineVisible(True)
self.setCaretLineBackgroundColor(QColor("#ffe4e4"))
# Set Python lexer
# Set style for Python comments (style number 1) to a fixed-width
# courier.
#
lexer = QsciLexerPython()
lexer.setDefaultFont(font)
self.setLexer(lexer)
text = bytearray(str.encode("Arial"))
# 32, "Courier New"
self.SendScintilla(QsciScintilla.SCI_STYLESETFONT, 1, text)
# Don't want to see the horizontal scrollbar at all
# Use raw message to Scintilla here (all messages are documented
# here: http://www.scintilla.org/ScintillaDoc.html)
self.SendScintilla(QsciScintilla.SCI_SETHSCROLLBAR, 0)
# not too small
self.setMinimumSize(600, 450)
def on_margin_clicked(self, nmargin, nline, modifiers):
# Toggle marker for the line the margin was clicked on
if self.markersAtLine(nline) != 0:
self.markerDelete(nline, self.ARROW_MARKER_NUM)
else:
self.markerAdd(nline, self.ARROW_MARKER_NUM)
if __name__ == "__main__":
app = QApplication(sys.argv)
editor = SimplePythonEditor()
editor.show()
editor.setText(open(sys.argv[0]).read())
app.exec_()
Run Code Online (Sandbox Code Playgroud)
只需将此代码复制粘贴到一个空.py文件中,然后运行它。您应该在显示器上看到以下简单的文本编辑器:
注意语法高亮是多么完美!QScintilla 当然在后台做了一些解析来实现这一点。
是否可以为此文本编辑器制作可点击的函数和变量?每个自尊的 IDE 都有它。您单击一个函数,IDE 将跳转到函数定义。变量也是一样。我想知道:
编辑:
?用户注意到以下内容:
可点击的函数名称需要对编程语言有更深入的了解 [..]
这超出了 Scintilla/QScintilla 的范围。Scintilla 提供了一种在鼠标单击文本上某处时做出反应的方法,但“函数定义在哪里”的逻辑不在 Scintilla 中,而且可能永远不会。
但是,有些项目专门用于此任务,例如ctags。您可以简单地围绕这种工具编写一个包装器。
我想为ctags编写这样的包装器现在在我的 TODO 列表中。第一步是在用户单击函数或变量时获得反应(Qt 信号)。也许当您将鼠标悬停在函数/变量上时,该函数/变量应该变蓝一点,以通知用户它是可点击的。我已经尝试实现这一点,但由于缺乏 QScintilla 文档而受阻。
因此,让我们将问题缩减为:如何使 QScintilla 文本编辑器中的函数或变量可点击(可点击定义为“发生了一些事情”)
编辑:
我现在才回到这个问题 - 几个月后。我一直在和我的朋友 Matic Kukovec 合作设计一个关于 QScintilla 的网站。这是一个关于如何使用它的初学者友好教程:
我希望这一举措填补了缺乏文档的空白。
小智 2
一种使用 Pyqt5 的方法,带有可点击函数和变量的选项。引用了可点击部分的脚本在带有自定义信号的 PyQt5 中看起来像这样。
PyQt4信号
self.connect(self,SIGNAL('marginClicked(int, int, Qt::KeyboardModifiers)'),
self.on_margin_clicked)
Run Code Online (Sandbox Code Playgroud)
PyQt5信号
self.marginClicked.connect(self.on_margin_clicked)
Run Code Online (Sandbox Code Playgroud)
PyQt5
import sys
import sip
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.Qsci import QsciScintilla, QsciLexerPython
class SimplePythonEditor(QsciScintilla):
ARROW_MARKER_NUM = 8
def __init__(self, parent=None):
super(SimplePythonEditor, self).__init__(parent)
# Set the default font
font = QFont()
font.setFamily('Courier')
font.setFixedPitch(True)
font.setPointSize(10)
self.setFont(font)
self.setMarginsFont(font)
# Margin 0 is used for line numbers
fontmetrics = QFontMetrics(font)
self.setMarginsFont(font)
self.setMarginWidth(0, fontmetrics.width("00000") + 6)
self.setMarginLineNumbers(0, True)
self.setMarginsBackgroundColor(QColor("#cccccc"))
# Clickable margin 1 for showing markers
self.setMarginSensitivity(1, True)
self.marginClicked.connect(self.on_margin_clicked)
self.markerDefine(QsciScintilla.RightArrow,
self.ARROW_MARKER_NUM)
self.setMarkerBackgroundColor(QColor("#ee1111"),
self.ARROW_MARKER_NUM)
# Brace matching: enable for a brace immediately before or after
# the current position
#
self.setBraceMatching(QsciScintilla.SloppyBraceMatch)
# Current line visible with special background color
self.setCaretLineVisible(True)
self.setCaretLineBackgroundColor(QColor("#ffe4e4"))
# Set Python lexer
# Set style for Python comments (style number 1) to a fixed-width
# courier.
#
lexer = QsciLexerPython()
lexer.setDefaultFont(font)
self.setLexer(lexer)
text = bytearray(str.encode("Arial"))
# 32, "Courier New"
self.SendScintilla(QsciScintilla.SCI_STYLESETFONT, 1, text)
# Don't want to see the horizontal scrollbar at all
# Use raw message to Scintilla here (all messages are documented
# here: http://www.scintilla.org/ScintillaDoc.html)
self.SendScintilla(QsciScintilla.SCI_SETHSCROLLBAR, 0)
# not too small
self.setMinimumSize(600, 450)
def on_margin_clicked(self, nmargin, nline, modifiers):
# Toggle marker for the line the margin was clicked on
if self.markersAtLine(nline) != 0:
self.markerDelete(nline, self.ARROW_MARKER_NUM)
else:
self.markerAdd(nline, self.ARROW_MARKER_NUM)
if __name__ == "__main__":
app = QApplication(sys.argv)
editor = SimplePythonEditor()
editor.show()
editor.setText(open(sys.argv[0]).read())
app.exec_()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4022 次 |
| 最近记录: |