I was expecting autocomplete suggestions of Python keywords (i.e. for, range, lambda
) and hoping for autocomplete suggestions with standard library modules and functions (i.e. sys, os.path
, etc.) when setting up an api with the QsciLexerPython lexer, but in the code example below, compressed and updated from this website, the only auto-completion that happens are the four strings added to the list.
Am I doing something wrong, and if so how do I get python keywords to register in the autocomplete? (My main question here of course)
If rather this is the expected behavior, what role does the lexer play in the autocomplete and why are the keywords from the lexer not being assigned to the autocomplete lists in this example?
Lastly, is it possible to have python standard library modules and sub-modules in the autocomplete, and what does that require?
"""Base code originally from: http://kib2.free.fr/tutos/PyQt4/QScintilla2.html"""
import sys
from PyQt5 import QtWidgets, Qsci
app = QtWidgets.QApplication(sys.argv)
editor = Qsci.QsciScintilla()
lexer = Qsci.QsciLexerPython()
editor.setLexer(lexer)
## setup autocompletion
api = Qsci.QsciAPIs(lexer)
api.add("aLongString")
api.add("aLongerString")
api.add("aDifferentString")
api.add("sOmethingElse")
api.prepare()
editor.setAutoCompletionThreshold(1)
editor.setAutoCompletionSource(Qsci.QsciScintilla.AcsAPIs)
editor.show()
editor.setText(open(sys.argv[0]).read())
sys.exit(app.exec_())
Thanks to ekhumoro for guiding me to the answer. The QsciAPIs class has a load method, and PyQt comes with a set of api files. Below is the code that does proper autocompletion in the manner I was looking for:
"""Base code originally from: http://kib2.free.fr/tutos/PyQt4/QScintilla2.html"""
import sys
import os
import PyQt5
from PyQt5 import QtWidgets, Qsci
app = QtWidgets.QApplication(sys.argv)
editor = Qsci.QsciScintilla()
lexer = Qsci.QsciLexerPython(editor)
editor.setLexer(lexer)
## setup autocompletion
api = Qsci.QsciAPIs(lexer)
# import the desired api file
pyqt_path = os.path.dirname(PyQt5.__file__)
api.load(os.path.join(pyqt_path, "Qt/qsci/api/python/Python-3.6.api"))
api.prepare()
editor.setAutoCompletionThreshold(1)
editor.setAutoCompletionSource(Qsci.QsciScintilla.AcsAll)
editor.show()
editor.setText(open(sys.argv[0]).read())
sys.exit(app.exec_())