使用 PyQt5 的语法高亮编辑器
Syntax highlighting editor with PyQt5
我目前正在开发一个基于 PyQt5 的应用程序,我可以使用一个(嵌入式)编辑器为 YAML(也许 JSON)提供一些语法高亮显示。
我希望 Qt 对此有一些内置功能,但我发现的只是一些讨论和一些手工实现,如 this one。
有没有一种简单的方法可以在现有小部件上激活语法高亮显示?或者我可能会使用一个紧凑的第 3 方小部件?
您可以使用 QsciScintilla
class with the QsciLexerJSON
and QsciLexerYAML
lexers of the QScintilla 模块。
import sys, os
from PyQt5 import QtWidgets, Qsci
JSON = """
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
"""
YAML = """
--- !clarkevans.com/^invoice
invoice: 34843
date : 2001-01-23
bill-to: &id001
given : Chris
family : Dumars
address:
lines: |
458 Walkman Dr.
Suite #292
city : Royal Oak
state : MI
postal : 48046
ship-to: *id001
product:
- sku : BL394D
quantity : 4
description : Basketball
price : 450.00
- sku : BL4438H
quantity : 1
description : Super Hoop
price : 2392.00
tax : 251.42
total: 4443.52
comments: >
Late afternoon is best.
Backup contact is Nancy
Billsmer @ 338-4338.
"""
class JSONEditor(Qsci.QsciScintilla):
def __init__(self, parent=None):
super().__init__(parent)
self.setLexer(Qsci.QsciLexerJSON(self))
self.setText(JSON)
class YAMLEditor(Qsci.QsciScintilla):
def __init__(self, parent=None):
super().__init__(parent)
self.setLexer(Qsci.QsciLexerYAML(self))
self.setText(YAML)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QWidget()
lay = QtWidgets.QHBoxLayout(w)
lay.addWidget(JSONEditor())
lay.addWidget(YAMLEditor())
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
我目前正在开发一个基于 PyQt5 的应用程序,我可以使用一个(嵌入式)编辑器为 YAML(也许 JSON)提供一些语法高亮显示。
我希望 Qt 对此有一些内置功能,但我发现的只是一些讨论和一些手工实现,如 this one。
有没有一种简单的方法可以在现有小部件上激活语法高亮显示?或者我可能会使用一个紧凑的第 3 方小部件?
您可以使用 QsciScintilla
class with the QsciLexerJSON
and QsciLexerYAML
lexers of the QScintilla 模块。
import sys, os
from PyQt5 import QtWidgets, Qsci
JSON = """
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
"""
YAML = """
--- !clarkevans.com/^invoice
invoice: 34843
date : 2001-01-23
bill-to: &id001
given : Chris
family : Dumars
address:
lines: |
458 Walkman Dr.
Suite #292
city : Royal Oak
state : MI
postal : 48046
ship-to: *id001
product:
- sku : BL394D
quantity : 4
description : Basketball
price : 450.00
- sku : BL4438H
quantity : 1
description : Super Hoop
price : 2392.00
tax : 251.42
total: 4443.52
comments: >
Late afternoon is best.
Backup contact is Nancy
Billsmer @ 338-4338.
"""
class JSONEditor(Qsci.QsciScintilla):
def __init__(self, parent=None):
super().__init__(parent)
self.setLexer(Qsci.QsciLexerJSON(self))
self.setText(JSON)
class YAMLEditor(Qsci.QsciScintilla):
def __init__(self, parent=None):
super().__init__(parent)
self.setLexer(Qsci.QsciLexerYAML(self))
self.setText(YAML)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QWidget()
lay = QtWidgets.QHBoxLayout(w)
lay.addWidget(JSONEditor())
lay.addWidget(YAMLEditor())
w.resize(640, 480)
w.show()
sys.exit(app.exec_())