Python wx.stc 自定义高亮显示

标签 python wxpython syntax-highlighting scintilla wxstyledtextctrl

由于在很多情况下都没有答案,而我在这里收到了很好的答案,这是我的问题。

我正在为 ABC 乐谱语言编写一个程序(示例如下)。这不是 wx.stc 中支持的语言,而且我还没有找到关于如何执行以下操作的良好解释或示例:

  1. 添加我自己的词法分析器/荧光笔
  2. 从头开始创建我自己的荧光笔并使用 RichTextCtrl 或类似工具。

欢迎任何方向的帮助。

ABC 符号示例:

%% this is a midi or postscript argument, and should be colored 
X: 1   % this is a comment and should be colored differently
T: Speed The Plough   % everything followed by a colon, except for |: should be colored
M: 4/4   % a variety of others should be colored
L: 1/8
R: reel
K: Dmaj
d3A BAFB|AF~F2 EFDE|F2AF ~G3B|   % the non-alphanumerics should be colored
ABdg fgfe|d3A BAFB|AF~F2 EFDE|
F2AF GABG|1ABdg fedc:|2ABde fdde||
|:f2ab afdf|g2fg ed (3Bcd|a2ab afdB|   % | and |: should be colored differently
ABde fe~e2|f2ab afdf|g2fg edBA|
[1F2AF GABG|ABdg fddg:|2F2AF ~G3B|ABdg fgfe||

最佳答案

要在 Python 中执行此操作,您需要将词法分析器设置为“容器”,然后处理“需要样式”事件:

self.SetLexer(wx.stc.STC_LEX_CONTAINER)
self.SetStyleBits(5)
self.Bind(wx.stc.EVT_STC_STYLENEEDED, self.handleStyleNeeded)

您在处理程序中具体执行的操作显然取决于您要突出显示的语法的细节。

为了获得灵感,我建议您熟悉 scintilla documentation on styling ,看看 source code一些更简单的内置词法分析器。

谷歌搜索也发现了this wx.stc example这可能会有帮助。

关于Python wx.stc 自定义高亮显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7807276/

相关文章:

python - 混合 Web/桌面应用程序 - Ruby on Rails 或 Django on Python?

python Pandas findall : dashed words and words

python - 在 Py3k 中,我如何让信号处理程序阻止信号的默认操作?

python - 用于运行脚本的 WxPython 按钮

python - 滚动面板鼠标滚轮滚动

javascript - 给定文件扩展名,如何为 Ace Editor 自动选择 "mode"

search - vim 按语法高亮类型搜索

python - 使用 Python 解析来自 AWS SDK 的 Cloudformation 字符串响应

python - 嵌入 matplotlib 图形并更改其大小

javascript - Highlight.js 不尊重子模式的父正则表达式