我想创建一个源代码编辑器,我需要创建一个自动完成功能。我一直在寻找示例代码,但我什么也没找到。:( 提前致谢。
问问题
1022 次
1 回答
2
最近在以下网站上描述了 QScintilla 的 API:
它非常适合初学者。我相信它可以帮助你!
这是一个自动完成示例,取自网站:
# ---------------------------------------------- #
# PYQT QSCINTILLA AUTOCOMPLETE #
# EXAMPLE #
# ---------------------------------------------- #
"""
Initialization
"""
# Import the PyQt5 module with some of the GUI widgets
import PyQt5.QtWidgets
# Import the QScintilla module
import PyQt5.Qsci
# Import Python's sys module needed to get the
# application arguments
import sys
# Create the main PyQt application object
application = PyQt5.QtWidgets.QApplication(sys.argv)
# Create a QScintila editor instance
editor = PyQt5.Qsci.QsciScintilla()
# Set the initial text
editor.setText("Inicialization example.")
# Append some text
editor.append("\nAdded text.")
"""
Customization - GENERAL
"""
# Disable the lexer
editor.setLexer(None)
# Set the encoding to UTF-8
editor.setUtf8(True)
# Set the End-Of-Line character to Unix style ('\n')
editor.setEolMode(PyQt5.Qsci.QsciScintilla.EolUnix)
# Make End-Of-Line characters visible
editor.setEolVisibility(True)
# Set the zoom factor, the factor is in points.
editor.zoomTo(4)
"""
Customization - LINE WRAPPING
"""
# Set the text wrapping mode to word wrap
editor.setWrapMode(PyQt5.Qsci.QsciScintilla.WrapWord)
# Set the text wrapping mode visual indication
editor.setWrapVisualFlags(PyQt5.Qsci.QsciScintilla.WrapFlagByText)
# Set the text wrapping to indent the wrapped lines
editor.setWrapIndentMode(PyQt5.Qsci.QsciScintilla.WrapIndentSame)
"""
Customization - EDGE MARKER
"""
# Set the edge marker's position and set it to color the background
# when a line goes over the limit of 50 characters
editor.setEdgeMode(PyQt5.Qsci.QsciScintilla.EdgeBackground)
editor.setEdgeColumn(50)
edge_color = caret_fg_color = PyQt5.QtGui.QColor("#ff00ff00")
editor.setEdgeColor(edge_color)
# Add a long line that will display the edge marker coloring
editor.append("\nSome long line that will display the edge marker's functionality.")
"""
Customization - INDENTATION
"""
# Set indentation with spaces instead of tabs
editor.setIndentationsUseTabs(False)
# Set the tab width to 4 spaces
editor.setTabWidth(4)
# Set tab indent mode, see the 3.3.4 chapter in QSciDocs
# for a detailed explanation
editor.setTabIndents(True)
# Set autoindentation mode to maintain the indentation
# level of the previous line (the editor's lexer HAS
# to be disabled)
editor.setAutoIndent(True)
# Make the backspace jump back to the tab width guides
# instead of deleting one character, but only when
# there are ONLY whitespaces on the left side of the
# cursor
editor.setBackspaceUnindents(True)
# Set indentation guides to be visible
editor.setIndentationGuides(True)
"""
Customization - CARET (the blinking cursor indicator)
"""
# Set the caret color to red
caret_fg_color = PyQt5.QtGui.QColor("#ffff0000")
editor.setCaretForegroundColor(caret_fg_color)
# Enable and set the caret line background color to slightly transparent blue
editor.setCaretLineVisible(True)
caret_bg_color = PyQt5.QtGui.QColor("#7f0000ff")
editor.setCaretLineBackgroundColor(caret_bg_color)
# Set the caret width of 4 pixels
editor.setCaretWidth(4)
"""
Customization - AUTOCOMPLETION (Partially usable without a lexer)
"""
# Set the autocompletions to case INsensitive
editor.setAutoCompletionCaseSensitivity(False)
# Set the autocompletion to not replace the word to the right of the cursor
editor.setAutoCompletionReplaceWord(False)
# Set the autocompletion source to be the words in the
# document
editor.setAutoCompletionSource(PyQt5.Qsci.QsciScintilla.AcsDocument)
# Set the autocompletion dialog to appear as soon as 1 character is typed
editor.setAutoCompletionThreshold(1)
# For the QScintilla editor to properly process events we
# need to add it to a QMainWindow object.
main_window = PyQt5.QtWidgets.QMainWindow()
# Set the central widget of the main window to
# be the editor
main_window.setCentralWidget(editor)
# Resize the main window and show it on the screen
main_window.resize(800, 600)
main_window.show()
# Execute the application
application.exec_()
于 2017-05-12T14:08:27.540 回答