【发布时间】:2021-02-08 07:23:07
【问题描述】:
我之前已经发布过这个问题,并且我已经能够将程序缩减为单个函数以用于测试目的。我没有收到任何错误,但我遇到了一个让我陷入困境的错误。我可以替换快捷方式,但它只会替换快捷方式的第一个字母。
代码如下:
from tkinter import *
root = Tk()
text = Text(root)
text.pack(expand=1, fill=BOTH)
syntax = "shortcut = sc" # This will be turned into a function to return the shortcut
# and the word, I'm only doing this for debugging purposes.
def replace_shortcut(event=None):
tokens = syntax.split()
word = tokens[:1]
shortcut = tokens[2:3]
index = '1.0'
while 1:
index = text.search(shortcut, index, stopindex="end")
if not index: break
last_idx = '%s + %dc' % (index, len(shortcut))
text.delete(index, last_idx)
text.insert(index, word)
last_idx = '%s + %dc' % (index, len(word))
text.bind('<space>', replace_shortcut)
text.mainloop()
给定的快捷方式,在我们的例子中,'sc' 将在输入空格后变成 'shortcutc'。任何帮助表示赞赏!
【问题讨论】:
-
你做了什么来调试这个?您是否已验证
index和last_idx是您所假设的?
标签: python tkinter desktop-application