Undo class

David Eppstein eppstein at ics.uci.edu
Mon Nov 10 11:08:59 EST 2003


In article <boobnq$69d$1 at news-reader3.wanadoo.fr>,
 Eric Brunel <eric.brunel at pragmadev.N0SP4M.com> wrote:

> > I'm developing a tool in Tkinter and would like to add Undo and Redo
> > commands to my Edit menu. Does somebody know if anybody has implemented
> > standard Undo/Redo as a Python module? I could not find any info with
> > Google on the matter.
> 
> On what king of widget? If it's a Text, you can use the native tk undo/redo 
> features: create the Text widget with the option undo set to 1, then use the 
> methods edit_undo() and edit_redo(). These are tk 8.4 features, so it may not 
> work for Python versions older than 2.3 (it doesn't work with 2.1; don't know 
> about 2.2)
> 
> For other widgets, I doubt there can be a generic mechanism to manage the 
> undo/redo functions for you: after all, only you can tell what actions should 
> be 
> able to be undone or redone. But I'd be really happy if someone can prove me 
> wrong!

The "only you can tell" argument is not convincing -- if you can tell, 
you can tell it to the undo manager.

Take a look at the Cocoa UndoManager functionality.
Basically, while you're handle a change to a UI object, you call
    U = undoManager.prepareWithInvocationTarget(self)
    U.methods(arguments...)
where self.methods(arguments...) would be the calls you'd do
to reverse the change, and it saves the method names and arguments for 
you. Later, when an undo is requested, it performs the calls you 
prototyped for it.  Very convenient, shouldn't be too hard to program in 
Python directly (in fact it is available in Python on OS X now, via 
PyObj).

-- 
David Eppstein                      http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science




More information about the Python-list mailing list