WxPython - saving items to ListCtrl

My question is, is it possible to save the items in the ListCtrl so that every time someone opens the app, the items are there, and if the user deletes it, it is also removed from the config. I know that I can use wx.Config and I am trying to use that, but I don’t know how to read it to accomplish what I want.

So what I would like to know is this is the correct way to write / read wx.Config in such a way that every time someone opens the application, there are items from the ListCtrl.

Thanks in advance.

+2


a source to share


1 answer


Using wx.Config is very easy, just create a config name for your application and add data eg.

config = wx.Config("StackOverflowTest")
config.Write("testdata", "yes it works!")

      

You can now read it anytime

config = wx.Config("StackOverflowTest")
print config.Read("testdata")

      



To save the cntrl data of the list, I would assume that you first read all the data in the python list and sort that list in config, next time reading config, print the data and fill the list, create wise functions like fillList / saveList so that you could make sure that the reading part to write is close and similar.

eg. you can use this skeleton

import wx
import cPickle

class MyListCtrl(wx.ListCtrl):

    def __init__(self, *args, **kwargs):
        wx.ListCtrl.__init__(self, *args, **kwargs)
        self.config = wx.Config("MykoolApp")

        self.fillist()

    def filllist(self):
         # load rows and check for error too, if no data
        data = self.config.Read("list_cntrl_data")
        rowList = cPickle.loads(data)

        for row in rowList:
            # add this row to list cntrl
            pass

    def savelist(self):
        rows = []
        for row in self:
            # add data to rows
            pass

        data =  cPickle.dumps(rows)
        self.config.Write("list_cntrl_data", data)

    def onchange(self):
        """
        on changes to list e.g. add delete call save list
        """
        self.savelist()

      

+3


a source







All Articles