撤消tkinter文本小部件中的功能

问题描述 投票:2回答:2

我试图在undo的文本小部件上使用tkinter函数,但没有任何运气。我这样试过:

from Tkinter import *
from ttk import Notebook

def OnVsb(*args):
    text.yview(*args)
    numbers.yview(*args)

def OnMouseWheel(event):
    text.yview("scroll", event.delta,"units")
    numbers.yview("scroll",event.delta,"units")
    return "break"

def undo(*argv):
    text.edit_undo()

root = Tk()
defaultbg = root.cget('bg')
root.bind('<Control-z>', undo)
note = Notebook(root)
frame = Frame(note, bd=5, relief=GROOVE, padx=5, pady=5)
frame.pack()
bar = Scrollbar(frame, command=OnVsb)
bar.pack(side=RIGHT, fill=Y)
numbers = Listbox(frame, width=5, height=30,bg=defaultbg,relief=FLAT,   yscrollcommand=bar.set)
numbers.pack(side=LEFT, fill=Y)
text = Text(frame,bd=3, width=145, height=30, yscrollcommand=bar.set)
text.pack(side=LEFT, fill=Y) 
text.bind("<MouseWheel>", OnMouseWheel)
text.tag_config("attr", foreground="tomato")
text.tag_config("value", foreground="dark violet")
text.tag_config("tags", foreground="dodger blue")
text.tag_config("text", font=("Georgia", "9", "bold"))
text.focus_set()
root.lift()
root.call('wm', 'attributes', '.', '-topmost', '1')
root.after_idle(root.call, 'wm', 'attributes', '.', '-topmost', False)
root.mainloop()

但由于某种原因,它什么也没做。我认为它是默认在文本小部件中实现的,但它没有用。有关如何在文本小部件上使用此功能的任何建议?任何例子都将非常感激。

python python-2.7 tkinter python-2.x text-widget
2个回答
7
投票

好的,我终于找到了这些信息。

所有我需要的是在我初始化文本小部件时将undo设置为True,就像这样:

text = Text(frame,bd=3, width=145, height=30, yscrollcommand=bar.set, undo=True)

没有必要undo功能和text.bind。当undoTrue时,它会自动运行。


0
投票

在Python 2中,必须将undo关键字arg设置为True以激活撤消/重做堆栈。在Python 3(至少3.6)中,默认情况下会激活堆栈。

© www.soinside.com 2019 - 2024. All rights reserved.