自动完成最后键入的单词

2024-09-29 17:20:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要创建一个GUI,让您键入短语,并尝试自动完成键入的每个单词,然后再次在其下打印输入。当我使用PyQt5中的QCompleter时,当我键入一个单词时,它工作正常,但当我键入第二个单词时,我无法自动完成它,因为它试图自动完成整个输入短语。我怎样才能对每个键入的单词给出建议

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.textEdited.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.console.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_())

Tags: fromimportselfinputmodel键入sys单词
1条回答
网友
1楼 · 发布于 2024-09-29 17:20:05

由于要添加文本,因此将QCompleter设置为QLineEdit将使任务复杂化,在这种情况下,需要独立处理QCompleter的逻辑

from functools import cached_property

from PyQt5.QtGui import QFont, QStandardItem, QStandardItemModel
from PyQt5.QtWidgets import QApplication, QCompleter, QLineEdit


class LineEdit(QLineEdit):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setFixedHeight(40)
        font = QFont("Open Sans", 12)
        self.setFont(font)
        self.completer.setWidget(self)
        self.completer.setModel(self.model)
        self.textChanged.connect(self.handle_text_changed)
        self.completer.activated.connect(self.handle_activated)

    @cached_property
    def model(self):
        return QStandardItemModel()

    @cached_property
    def completer(self):
        return QCompleter()

    def add_word(self, word):
        if not self.model.findItems(word):
            self.model.appendRow(QStandardItem(word))

    def handle_text_changed(self):
        text = self.text()[0 : self.cursorPosition()]
        if not text:
            self.completer.popup().hide()
            return
        words = text.split()
        if text.endswith(" "):
            for word in words:
                self.add_word(word)
            self.completer.popup().hide()
            return
        self.completer.setCompletionPrefix(words[-1])
        self.completer.complete()

    def handle_activated(self, text):
        prefix = self.completer.completionPrefix()
        extra = text[len(prefix) :]
        self.blockSignals(True)
        self.insert(extra)
        self.blockSignals(False)
        self.add_word(text)


def main():
    import sys

    app = QApplication(sys.argv)

    lineedit = LineEdit()
    lineedit.show()

    sys.exit(app.exec())


if __name__ == "__main__":
    main()

相关问题 更多 >

    热门问题