Adding functionality for autofill suggestion on text input in python

575 views Asked by At

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.

enter image description here

enter image description here

This is the second example for the same

enter image description here

enter image description here

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.

enter image description here

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:

enter image description here

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_())

enter image description here

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.

enter image description here

I have added 3 words and now it will give me recommendations based on them.

enter image description here

enter image description here

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.

1

There are 1 answers

1
Batselot On

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.

class AppDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.word_list=["apple","banana","question"]
        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.word_list, 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.word_list.appendRow(QStandardItem(entryItem))


app = QApplication(sys.argv)
demo = AppDemo()
demo.show()
sys.exit(app.exec_())