I found a simpler way to do it, see the following:
import pathlib
import sys
from PyQt5.QtGui import QFont, QFontMetrics, QColor
from PyQt5.QtWidgets import QApplication
from PyQt5.Qsci import QsciScintilla, QsciLexerPython
class SimplePythonEditor(QsciScintilla):
def __init__(self, parent=None):
super().__init__(parent)
# Set the default font
font = QFont()
font.setFamily("Courier")
font.setFixedPitch(True)
font.setPointSize(10)
self.setFont(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"))
lexer = QsciLexerPython()
lexer.setDefaultFont(font)
self.setLexer(lexer)
if __name__ == "__main__":
app = QApplication(sys.argv)
editor = SimplePythonEditor()
editor.show()
editor.setText(pathlib.Path(sys.argv[0]).read_text())
sys.exit(app.exec())