【发布时间】:2016-11-28 09:41:06
【问题描述】:
我正在寻找一种方法来更改 Tkinter 文本小部件中的退格键效果。我正在从 Tkinter 监视文本窗口中的关键事件,我需要找到一种方法:
禁用 Backspace 键的效果,但仍然可以知道它是输入的
删除用户打算删除的字符
我不知道从哪里开始。我可以挑出 Backspace 事件,但不知道如何防止它删除字符。我也不知道如何实时影响输入(在用户书写时对字符应用删除线“样式”)
这是我现在所拥有的,但这并不多:
from Tkinter import *
import tkFileDialog
root=Tk()
root.title("TextnonEdit")
text=Text(root)
text.grid()
def keepit(key):
print key.keysym
if key.keysym == "BackSpace":
print "do not delete, strikethrough"
# Bind entry to keypress
text.bind("<Key>", keepit)
def saveas():
global text
t = text.get("1.0", "end-1c")
savelocation=tkFileDialog.asksaveasfilename()
file1=open(savelocation, "w+")
file1.write(t)
file1.close()
button=Button(root, text="Save", command=saveas)
button.grid()
root.mainloop()
提前感谢您的任何回答!
【问题讨论】:
标签: python replace tkinter backspace strikethrough