def save(evt, designer):
    "Basic save functionality: just replaces the gui code"

    # ask the user if we should save the changes:
    ok = gui.confirm("Save the changes?", "GUI2PY Designer", 
                     cancel=True, default=True)
    if ok:
        wx_obj = evt.GetEventObject()
        w = wx_obj.obj
        try:
            if DEBUG: print "saving..."
            # make a backup:
            fin = open(designer.filename, "r")
            fout = open(designer.filename + ".bak", "w")
            fout.write(fin.read())
            fout.close()
            fin.close()
            if designer.filename.endswith(".rsrc.py"):
                # serialize and save the resource
                gui.save(designer.filename, [gui.dump(w)])
            else:
                # reopen the files to proccess them
                fin = open(designer.filename + ".bak", "r")
                fout = open(designer.filename, "w")
                copy = True
                newlines = fin.newlines or "\n"

                def dump(obj, indent=1):
                    "recursive convert object to string"
                    for ctl in obj[:]:
                        write(ctl, indent)

                def write(ctl, indent):
                    if ctl[:]:
                        fout.write(" " * indent * 4)
                        fout.write("with %s:" % ctl.__repr__(parent=None, indent=indent, context=True))
                        fout.write(newlines)
                        dump(ctl, indent + 1)                
                    else:
                        fout.write(" " * indent * 4)
                        fout.write(ctl.__repr__(parent=None, indent=indent))
                        fout.write(newlines)

                dumped = False
                for line in fin:
                    if line.startswith("# --- gui2py designer generated code starts ---"):
                        fout.write(line)
                        fout.write(newlines)
                        write(w, indent=0)
                        fout.write(newlines)
                        dumped = True
                        copy = False
                    if line.startswith("# --- gui2py designer generated code ends ---"):
                        copy = True
                    if copy:
                        fout.write(line)
                        #fout.write("\n\r")
                if not dumped:
                    gui.alert("No valid # --- gui2py... delimiters! \n"
                              "Unable to write down design code!", 
                              "Design not updated:")
            fout.close()
            fin.close()
        except Exception, e:
            import traceback
            print(traceback.print_exc())
            ok = gui.confirm("Close anyway?\n%s" % str(e), 'Unable to save:', 
                             ok=True, cancel=True)
    if ok is not None:
        wx.CallAfter(exit)    # terminate the designer program
    return ok
Basic save functionality: just replaces the gui code