如何检查 tkinter.Text 撤消堆栈?

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

Tk 文档讨论了撤消堆栈,但没有提到是否可以直接检查它。

当撤消堆栈耗尽时,我想将撤消命令显示为非活动状态。跟踪 undo-s 和 redo-s 是我唯一的机会,还是有办法询问 tk 堆栈是否为空?

tkinter tk-toolkit
2个回答
2
投票

无法显式检查撤消堆栈。但是,您也许可以使用

modified
标志来达到类似的目的。

来自官方tk文档

撤消机制也与修改后的标志相关联。这意味着 撤消或重做更改可以恢复修改后的文本小部件 到未修改状态,反之亦然。将设置修改后的标志 自动进入合适的状态。这种自动耦合可以 当用户设置了修改标志时不起作用,直到 标志已再次重置。


0
投票

要检查是否可以撤消/重做,可以使用

text.edit("canundo")
text.edit("canredo")

这来自

tcl/tk
文档(自版本 8.6 起):

路径名称编辑可以重做
如果可以重做,即重做堆栈不为空,则返回布尔值 true。否则返回 false。
路径名称 编辑 canundo
如果可以撤消,即当撤消堆栈不为空时,则返回布尔值 true。否则返回 false。

它还没有作为函数内置到

tkinter
中,所以我们必须使用内部方法:

def edit(self, *args):
    """Internal method

    This method controls the undo mechanism and
    the modified flag. The exact behavior of the
    command depends on the option argument that
    follows the edit argument. The following forms
    of the command are currently supported:

    edit_modified, edit_redo, edit_reset, edit_separator
    and edit_undo

    """
    return self.tk.call(self._w, 'edit', *args)
© www.soinside.com 2019 - 2024. All rights reserved.