在 Tkinter 文本小部件中更改退格效果
Changing Backspace effect in Tkinter text widget
我正在寻找一种方法来更改 Tkinter 文本小部件中的退格键效果。我正在监视来自 Tkinter 的文本 Window 中的关键事件,我需要找到一种方法来:
禁用退格键的效果,但仍然可以知道它被键入
删除用户打算删除的字符
我不知道从哪里开始。我可以挑出 Backspace 事件,但不知道如何防止它删除一个字符。我也不知道如何实时影响输入(在用户书写时对字符应用删除线 'style')
这是我目前拥有的,但并不多:
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()
提前感谢您的回答!
问题可以通过以下方式解决:
- 在文本小部件上创建一个标签,并将
overstrike
属性设置为 true
- 在退格字符上创建绑定以应用标签
- 具有绑定函数return
"break"
以防止默认行为
示例:
import Tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.text = tk.Text(self)
self.text.pack(fill="both", expand=True)
# add a tag that lets us apply the overstrike attribute
self.text.tag_configure("overstrike", overstrike=True)
# add a binding on the backspace key
self.text.bind("<BackSpace>", self.handleBackspace)
def handleBackspace(self, event):
# add the overstrike to the character before the
# insertion cursor
self.text.tag_add("overstrike", "insert-1c", "insert")
# move the cursor to the previous position
self.text.mark_set("insert", "insert-1c")
# prevent the default behaviour
return "break"
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()
谢谢布莱恩!这是经过一些调整后我得到的结果,它可以连续使用多个退格键并删除选择:
import Tkinter as tk
x = 0
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.text = tk.Text(self)
self.text.pack(fill="both", expand=True)
# add a tag that lets us apply the overstrike attribute
self.text.tag_configure("overstrike", overstrike=True)
self.text.bind("<Key>", self.eyeout)
# add a binding on the backspace key
self.text.bind("<BackSpace>", self.handleBackspace)
def eyeout(self, key):
global x
print key.keysym
# move the cursor to the previous position
self.text.mark_set("insert", "insert+" + str(x) + "c")
print "insert+" + str(x) + "c"
#reset consecutive backspace count to zero
x = 0
def handleBackspace(self, event):
if self.text.tag_ranges(tk.SEL):
print ('Selected text !')
#if text is selected, overstrike all the selection
self.text.tag_add("overstrike", "sel.first", "sel.last")
# move cursor to end of selection
self.text.mark_set("insert", "sel.last")
# deselect to avoid overwrite
self.text.tag_remove(tk.SEL, "sel.first", "sel.last")
# counting doesn't apply to selection, reset backspace count
global x
x = 0
else:
print('NO Selected Text')
# add the overstrike to the character before the
# insertion cursor
self.text.tag_add("overstrike", "insert-1c", "insert")
# counting system to know how many backspaces in a row were entered
print "Backspace!"
print x
global x
x = x+1
print x
print "Backspace increase"
self.text.mark_set("insert", "insert-1c")
# prevent the default behaviour
return "break"
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()
我正在寻找一种方法来更改 Tkinter 文本小部件中的退格键效果。我正在监视来自 Tkinter 的文本 Window 中的关键事件,我需要找到一种方法来:
禁用退格键的效果,但仍然可以知道它被键入
删除用户打算删除的字符
我不知道从哪里开始。我可以挑出 Backspace 事件,但不知道如何防止它删除一个字符。我也不知道如何实时影响输入(在用户书写时对字符应用删除线 'style')
这是我目前拥有的,但并不多:
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()
提前感谢您的回答!
问题可以通过以下方式解决:
- 在文本小部件上创建一个标签,并将
overstrike
属性设置为 true - 在退格字符上创建绑定以应用标签
- 具有绑定函数return
"break"
以防止默认行为
示例:
import Tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.text = tk.Text(self)
self.text.pack(fill="both", expand=True)
# add a tag that lets us apply the overstrike attribute
self.text.tag_configure("overstrike", overstrike=True)
# add a binding on the backspace key
self.text.bind("<BackSpace>", self.handleBackspace)
def handleBackspace(self, event):
# add the overstrike to the character before the
# insertion cursor
self.text.tag_add("overstrike", "insert-1c", "insert")
# move the cursor to the previous position
self.text.mark_set("insert", "insert-1c")
# prevent the default behaviour
return "break"
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()
谢谢布莱恩!这是经过一些调整后我得到的结果,它可以连续使用多个退格键并删除选择:
import Tkinter as tk
x = 0
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.text = tk.Text(self)
self.text.pack(fill="both", expand=True)
# add a tag that lets us apply the overstrike attribute
self.text.tag_configure("overstrike", overstrike=True)
self.text.bind("<Key>", self.eyeout)
# add a binding on the backspace key
self.text.bind("<BackSpace>", self.handleBackspace)
def eyeout(self, key):
global x
print key.keysym
# move the cursor to the previous position
self.text.mark_set("insert", "insert+" + str(x) + "c")
print "insert+" + str(x) + "c"
#reset consecutive backspace count to zero
x = 0
def handleBackspace(self, event):
if self.text.tag_ranges(tk.SEL):
print ('Selected text !')
#if text is selected, overstrike all the selection
self.text.tag_add("overstrike", "sel.first", "sel.last")
# move cursor to end of selection
self.text.mark_set("insert", "sel.last")
# deselect to avoid overwrite
self.text.tag_remove(tk.SEL, "sel.first", "sel.last")
# counting doesn't apply to selection, reset backspace count
global x
x = 0
else:
print('NO Selected Text')
# add the overstrike to the character before the
# insertion cursor
self.text.tag_add("overstrike", "insert-1c", "insert")
# counting system to know how many backspaces in a row were entered
print "Backspace!"
print x
global x
x = x+1
print x
print "Backspace increase"
self.text.mark_set("insert", "insert-1c")
# prevent the default behaviour
return "break"
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()