The Qt library has a QCompleter class, which allows you to offer autocompletion to the input word in the input fields. This class is also supported by the PyQt5 library.
In the minimum, the application of this class might look like this:
# Create an input field lineEdit = QLineEdit(self) strList = ['Python', 'PyQt5', 'Qt', 'Django', 'QML'] # Create a list of words # We create QCompleter, in which we establish the list, and also the pointer to the parent completer = QCompleter(strList, lineEdit) lineEdit.setCompleter(completer) # Set QCompleter in the input field
For application in the program, this code can be implemented as follows:
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QGridLayout, QWidget, QLineEdit, QCompleter from PyQt5.QtCore import QSize class MainWindow(QMainWindow): # Override class constructor def __init__(self): # You must call the super class method QMainWindow.__init__(self) self.setMinimumSize(QSize(480, 80)) # Set sizes self.setWindowTitle("Line Edit IP Address") # Set the window title central_widget = QWidget(self) # Create a central widget self.setCentralWidget(central_widget) # Install the central widget grid_layout = QGridLayout(self) # Create QGridLayout central_widget.setLayout(grid_layout) # Set this accommodation in central widget grid_layout.addWidget(QLabel("Autocompletion check", self), 0, 0) # Create an input field lineEdit = QLineEdit(self) strList = ['Python', 'PyQt5', 'Qt', 'Django', 'QML'] # Create a list of words # We create QCompleter, in which we establish the list, and also the pointer to the parent completer = QCompleter(strList, lineEdit) lineEdit.setCompleter(completer) # Set QCompleter in the input field grid_layout.addWidget(lineEdit, 0, 1) # Add the input field to the grid if __name__ == "__main__": import sys app = QApplication(sys.argv) mw = MainWindow() mw.show() sys.exit(app.exec())
Большое спасибо.
Вы очень мне помогли.
Пожалуйста ))
Будут какие затруднения, задавайте вопросы на форуме в разделе PyQt5. PyQt5 не является моей рабочей нишей, но я работаю периодически с python, а сама библиотека Qt - это моё основное направление. Так что, если будет возможным, отвечу на вопросы или направлю, куда копать. Да и сообщество может поможет, ну и сами не стесняйтесь отвечать на вопросы других пользователей.