0

I am writing a wxpython GUI application where I have used wxribbon which is dynamic in nature. It(the ribbon) allows users to add panels and then in each panel add buttons according to their need. Now when user closes the window, I want to save the current state of ribbon with the number and names of tabs, panels(in each tab) and then buttons(in each panel) and then load it when user again runs the application. I am able to save only page till now as I am not able to find a correct way to nest for loops while writing json file. Here is a sample code of what I am doing. Any help will be great.. Thank You...

def save(self):
    pages = [{'label': child.GetLabel()}
             for child in self._ribbon.GetChildren()
             if isinstance(child, RB.RibbonPage)]

    #panels = [] #It will be nested above inside each page
    #buttons = [] #It will be nested inside each panel

   activepage = self._ribbon.GetActivePage()

    data = {
        'activepage':activepage,
        'pages':pages,
        }
    with open(CONFIGFILE, 'w') as f:
        json.dump(data, f)


def load(self):
    with open(CONFIGFILE, 'r') as f:
        data = json.load(f)

    for Page in data['pages']:
        label = Page['label']
        newpage = RB.RibbonPage(self._ribbon,wx.ID_ANY, Page['label'],Bitmap("eye.xpm"))

    currentpage = data['activepage']
    self._ribbon.SetActivePage(currentpage)
    self._ribbon.Realize()
wnnmaw
  • 5,444
  • 3
  • 38
  • 63
Samyak
  • 109
  • 2
  • 8

1 Answers1

0

I think that when you say "recursively" you are asking how to do something like this:

   def save(self):
       pages = [{'label': child.GetLabel(),
                 'panels' : [{'whatever': panel.GetWhatever()
                              'whatever_else': panel.GetWhateverElse()}
                             for panel in child.GetChildren()
                             if isinstance(panel, wx.Panel)]
                 for child in self._ribbon.GetChildren()
                 if isinstance(child, RB.RibbonPage)]

IE - this is a way to include all the panel information for each child of the ribbon along with the label information about that child.

(Note that of course 'whatever' and 'whatever_else' are the things that you need to know about the Panel to recreate it - only you probably know those things.)

Note also that this is not "recursive", it is "nested".

However I can't help feeling that this approach is "digging a hole for yourself" in terms of code complexity. To keep it easy to understand and maintain I would tend to do:

 def save(self):
    pages = []

    for child in self._ribbon.GetChildren:
        next if not isinstance(child, RB.RibbonPage)

        label = child.GetLabel()
        panels = [{'whatever': panel.GetWhatever()
                   'whatever_else': panel.GetWhateverElse()}
                  for panel in child.GetChildren()
                  if isinstance(panel, wx.Panel)]

        pages.append({'label': label,
                      'panels': panels})

I've written enough nested list comprehensions or similar in the past, like my first example, then come back to them later and not thanked myself for trying to be clever because it's too hard to read, that now I'd choose the latter way of doing it, even if it isn't funky python.n By the time you add buttons as well as panels, I think you will see what I mean :)

GreenAsJade
  • 14,459
  • 11
  • 63
  • 98