I have tried several ways to add autocomplete/autofill suggestion functionality on text input. I have set a list 'names' for which I want autofill suggestions when being searched. I have tried three different codes for the same. I am facing problem in the three of them.
First code I got from pypi.org (https://pypi.org/project/fast-autocomplete/). It has library named fast-autocomplete. I tried using it but the problem I am facing here is that it is not giving me real-time autocomplete suggestions.
from fast_autocomplete import AutoComplete
words = {'book': {}, 'burrito': {}, 'pizza': {}, 'pasta':{}}
autocomplete = AutoComplete(words=names)
autocomplete.search(input('Type the word \n'), max_cost=3, size=3)
The output coming, is that when I type in the input, it is not giving me realtime suggestions. Once I have entered the word, then it is displaying the suggestions. I want the suggestions to be displayed realtime based on list.
This is the second example for the same
The second code I tried was from pythonbasics.org (https://pythonbasics.org/pyqt-auto-complete/). Here I am unable to get the desired output.
This is the second code:
from PyQt5.QtWidgets import *
import sys
class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
layout = QGridLayout()
self.setLayout(layout)
# auto complete options
names = ["Apple", "Alps", "Berry", "Cherry" ]
completer = QCompleter(names)
# create line edit and add auto complete
self.lineedit = QLineEdit()
self.lineedit.setCompleter(completer)
layout.addWidget(self.lineedit, 0, 0)
app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec_())
In this the undesirable output is coming. I am getting autocomplete suggestions for words in the list 'names' but I am unable to access it. Only suggestions are coming but when I want to click, nothing is happening. Once clicked, I want to get it stored in a variable which I can access later. I have given the ouput for your perusal.
Here in this image, when I typed A, I am getting autofill suggestions as Alps or Apple which I want but when I click it, nothing happens. This happens when i click it:
I have clicked apple and nothing happens, I don't know where Apple is getting stored or how it is functioning exactly. Once clicked, I want further operations to be performed.
In the third code, I am getting a layout from which I can add my own words and later on, I am getting suggestions depending on the words added by me. I want it to connect to my list but I am unable to do so.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTextEdit, QLineEdit, QCompleter
from PyQt5.QtGui import QStandardItem, QStandardItemModel, QFont
class AppDemo(QWidget):
def __init__(self):
super().__init__()
self.resize(1200, 800)
fnt = QFont('Open Sans', 12)
mainLayout = QVBoxLayout()
# input field
self.input = QLineEdit()
self.input.setFixedHeight(50)
self.input.setFont(fnt)
self.input.editingFinished.connect(self.addEntry)
mainLayout.addWidget(self.input)
self.model = QStandardItemModel()
completer = QCompleter(self.model, self)
self.input.setCompleter(completer)
self.console = QTextEdit()
self.console.setFont(fnt)
mainLayout.addWidget(self.console)
self.setLayout(mainLayout)
def addEntry(self):
entryItem = self.input.text()
self.input.clear()
self.console.append(entryItem)
if not self.model.findItems(entryItem):
self.model.appendRow(QStandardItem(entryItem))
app = QApplication(sys.argv)
demo = AppDemo()
demo.show()
sys.exit(app.exec_())
In the above image, an empty layout is appearing through which I can add my own word through input field and later on it will give me recommendations based on those words.
I have added 3 words and now it will give me recommendations based on them.
Since I have added Apple and Banana, it is giving me recommendation based on them, I want it to connect it to my own list which can be searched. I don't want any word to be added but only require autofill/autocomplete functionality based on list for text field input. Just like how Google works. Once I get my word based on autocomplete suggestions, I want to click it and get my desired data displayed.










I think this works as a minimal solution. You can define a list like self.word_list and it can recommend you the words of your choosing.