我需要创建一个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_())
由于要添加文本,因此将QCompleter设置为QLineEdit将使任务复杂化,在这种情况下,需要独立处理QCompleter的逻辑
相关问题 更多 >
编程相关推荐