def __init__(self): """ Invoked to return the default editor widget. """ from PyQt5.Qsci import QsciLexerPython, QsciScintilla editor = QsciScintilla() # Configure the editor for Python. editor.setUtf8(True) editor.setLexer(QsciLexerPython(editor)) editor.setFolding(QsciScintilla.PlainFoldStyle, 1) editor.setIndentationGuides(True) editor.setIndentationWidth(4) self.editor = editor
class highlightEditor(QMainWindow): """ 语法高亮代码框 """ def __init__(self, parent=None, lineNumberOn=False): super(highlightEditor, self).__init__() self.editor = QsciScintilla(parent) font = QFont() font.setFamily("Consolas") font.setPointSize(12) font.setFixedPitch(True) self.editor.setFont(font) self.editor.setObjectName("editor") self.editor.setUtf8(True) self.editor.setMarginsFont(font) if lineNumberOn: self.editor.setMarginWidth( 0, len(str(len(self.editor.text().split('\n')))) * 20) self.editor.setMarginLineNumbers(0, lineNumberOn) self.editor.setBraceMatching(QsciScintilla.StrictBraceMatch) self.editor.setIndentationsUseTabs(True) self.editor.setIndentationWidth(4) self.editor.setTabIndents(True) self.editor.setAutoIndent(True) self.editor.setBackspaceUnindents(True) self.editor.setTabWidth(4) self.editor.setCaretLineVisible(True) self.editor.setCaretLineBackgroundColor(QColor('#DCDCDC')) self.editor.setIndentationGuides(True) self.editor.setFolding(QsciScintilla.PlainFoldStyle) self.editor.setMarginWidth(2, 12) self.editor.markerDefine(QsciScintilla.Minus, QsciScintilla.SC_MARKNUM_FOLDEROPEN) self.editor.markerDefine(QsciScintilla.Plus, QsciScintilla.SC_MARKNUM_FOLDER) self.editor.markerDefine(QsciScintilla.Minus, QsciScintilla.SC_MARKNUM_FOLDEROPENMID) self.editor.markerDefine(QsciScintilla.Plus, QsciScintilla.SC_MARKNUM_FOLDEREND) self.editor.setMarkerBackgroundColor( QColor("#FFFFFF"), QsciScintilla.SC_MARKNUM_FOLDEREND) self.editor.setMarkerForegroundColor( QColor("#272727"), QsciScintilla.SC_MARKNUM_FOLDEREND) self.editor.setMarkerBackgroundColor( QColor("#FFFFFF"), QsciScintilla.SC_MARKNUM_FOLDEROPENMID) self.editor.setMarkerForegroundColor( QColor("#272727"), QsciScintilla.SC_MARKNUM_FOLDEROPENMID) self.editor.setAutoCompletionSource(QsciScintilla.AcsAll) self.editor.setAutoCompletionCaseSensitivity(True) self.editor.setAutoCompletionReplaceWord(False) self.editor.setAutoCompletionThreshold(1) self.editor.setAutoCompletionUseSingle(QsciScintilla.AcusExplicit) self.lexer = highlight(self.editor) self.editor.setLexer(self.lexer) self.__api = QsciAPIs(self.lexer) autocompletions = keyword.kwlist + [] for ac in autocompletions: self.__api.add(ac) self.__api.prepare() self.editor.autoCompleteFromAll() self.editor.textChanged.connect(self.changed) def changed(self): self.editor.setMarginWidth( 0, len(str(len(self.editor.text().split('\n')))) * 20)