我已編輯的代碼的基礎上,這兩個來源,對我來說,我覺得這個工作得更好:
def handle_keyrelease(self, event):
"""event handler for the keyrelease event on this widget"""
if event.keysym == "BackSpace":
if self.position < self.index(END): # delete the selection
self.delete(self.position, END)
else:
#self.delete(self.index(INSERT), END)
self.position = self.index(END)
if event.keysym == "Left":
if self.position < self.index(END): # delete the selection
self.delete(self.position, END)
#else:
#self.position = self.position-1 # delete one character
#self.delete(self.position, END)
if event.keysym == "Right":
self.position = self.index(END) # go to end (no selection)
if event.keysym == "Down":
self.autocomplete(1) # cycle to next hit
if event.keysym == "Up":
self.autocomplete(-1) # cycle to previous hit
# perform normal autocomplete if event is a single key or an umlaut
if len(event.keysym) == 1:
self.autocomplete()
基本上,這是伊恩·魏瑟爾的版本。我在「backspace」鍵部分添加了if
子句,並刪除了上面的註釋行,以便用戶返回並更正一些拼寫錯誤。現在,如果用戶返回並編輯了某些內容,則先前輸入的其他文本將保持不變,如果用戶按下(僅刪除剩餘的自動完成但保留其餘的完整內容)或退格(刪除自動完成和一個字符如預期的那樣)。如果用戶在文本中單擊鼠標,則不會刪除任何內容。