Persistent objects are simply the objects which automatically save their state when they are destroyed and restore it when they are recreated, even during another program invocation.
Most often, persistent objects are, in fact, persistent windows as it is especially convenient to automatically restore the UI state when the program is restarted but an object of any class can be made persistent. Moreover, persistence is implemented in a non-intrusive way so that the original object class doesn’t need to be modified at all in order to add support for saving and restoring its properties.
The persistence framework involves:
wxPython has built-in support for a (constantly growing) number of controls. Currently the following classes are supported:
To automatically save and restore the properties of the windows of classes listed above you need to:
Example of using a notebook control which automatically remembers the last open page:
import wx, os
import wx.lib.agw.persist as PM
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, "Persistent Controls Demo")
self.book = wx.Notebook(self, wx.ID_ANY)
# Very important step!!
self.book.SetName("MyBook") # Do not use the default name!!
self.book.AddPage(wx.Panel(self.book), "Hello")
self.book.AddPage(wx.Panel(self.book), "World")
self.Bind(wx.EVT_CLOSE, self.OnClose)
self._persistMgr = PM.PersistenceManager.Get()
_configFile = os.path.join(os.getcwd(), self.book.GetName())
self._persistMgr.SetPersistenceFile(_configFile)
if not self._persistMgr.RegisterAndRestoreAll(self.book):
# Nothing was restored, so choose the default page ourselves
self.book.SetSelection(0)
def OnClose(self, event):
self._persistMgr.SaveAndUnregister(self.book)
event.Skip()
# our normal wxApp-derived class, as usual
app = wx.App(0)
frame = MyFrame(None)
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()
User-defined classes can be easily integrated with PersistenceManager. To add support for your custom class MyWidget you just need to:
AbstractHandler
;If you want to add persistence support for a class not deriving from wx.Window, you need to derive MyPersistentWidget directly from PersistentObject and so implement its PersistentObject.GetName() method too. Additionally, you must ensure that PersistenceManager.SaveAndUnregister() is called when your object is destroyed as this can be only done automatically for windows.
ToolBar
UI settings as it has been done for AuiToolBar
:
current ToolBar
doesn’t seem to have easy access to the underlying toolbar tools;grid.Grid
for row/columns sizes (possibly adding another style
to PersistenceManager
as grid.Grid
sets up arrays to store individual row and column
sizes when non-default sizes are used. The memory requirements for this could become prohibitive
if the grid is very large);wx.ColourDialog
, wx.FontDialog
etc...);lib
).PersistentObjects library is distributed under the wxPython license.
Latest revision: Andrea Gavana @ 27 Mar 2013, 21.00 GMT Version 0.5.
persist_constants |
This module contains all the constants used by the persistent objects. |
persist_handlers |
This module contains different classes which handle different kind of saving/restoring |
persistencemanager |
This module contains the definitions of PersistentObject |