tkinter中undo的使用
时间: 2023-11-20 13:53:05 浏览: 256
在tkinter中,undo是指撤销操作,即撤销之前的操作。可以通过设置Text组件的undo属性为True来激活撤销/恢复功能。同时,可以使用edit_separator()方法在文本编辑器中插入一个分隔符,以便于在需要时撤销/恢复操作。可以使用edit_undo()方法撤销上一次操作,使用edit_redo()方法恢复上一次操作。在Text组件中,可以通过maxundo属性设置最大的撤销次数。下面是一个简单的例子,演示了如何在tkinter中使用undo功能:
<<EG:>>
from tkinter import *
root = Tk()
# 'undo=True' 激活撤销/恢复功能
# 'autoseparators=False' 关闭自动设置separator设置
text = Text(root, width=30, height=10, undo=True, autoseparators=False, maxundo=10)
text.pack()
def typing(event):
text.edit_separator()
def undo():
text.edit_undo()
def redo():
text.edit_redo()
text.bind('<Key>', typing) #绑定键盘按键,每次点击都设置separator,方便undo/redo
text.insert('1.0', 'Love is burn into peace')
Button(root, text='UNDO', command=undo).pack()
Button(root, text='REDO', command=redo).pack()
mainloop()
阅读全文
相关推荐


















