[pygtk] Undo and Redo

N. Volbers mithrandir42 at web.de
Sun Sep 4 15:51:11 WST 2005

Jens Geiregat schrieb:

>I would like to add undo and redo buttons to my program. The problem
>is I do not have any idea how to implement the undo and redo-actions.
>Are there some (easy) examples on this subject? Or other (small)
>pygtk-programs that support undo and redo?

Hello Jens,

your question is a very interesting one! I had the same problem about a
year ago when I started writing my application in python.  As a starting
point, I would advise you to look at the following three sources:

1. Skencil, a vector drawing program: http://www.nongnu.org/skencil/ by
Bernhard Herzog.

Look at "Documentation" and then at the "Developer's Guide".  This is a
very nice introduction to an undo mechanism that basically works like
this:  If you call an undoable function, then it must return an
undo-tuple which contains the name of a function f and the appropriate
arguments x,y,..., so that f(x,y,...) will undo the last action.  This
will also enable you to offer a "redo" mechanism for free!

The only problem I had with this approach was that you waste the
possibility to use the return value!

2. SloppyPlot, a plotting program, written by myself :-),

It includes an Undo library (Sloppy.Lib.Undo) which works similar to the
one found in Skencil, with the difference, that the undo information is
appended to an undo list, which is passed to the function as keyword

def my_function(arg1, arg2, undolist=[]):
    undolist.append( ...undo information... )

The benefit of this approach is, that you can still return any value you
like.  Also, you can decide not to use the undo mechanism by simply not
passing any undolist to the function.  In this case, the undo
information is appended to the [] list and is basically discarded!  If
you need more information, feel free to contact me.

3. Yet another approach is used by gazpacho (google for link), where for
every action there is a wrapper with a do and an undo function.

Hope this helps,


More information about the pygtk mailing list