Search code examples
pythonwxpythonwizarddynamically-generated

wxPython dynamically add pages to wizard


I have been working on developing a wxPython based wizard which I would like to be capable of dynamically increasing in size based on input provided within the wizard itself. This wizard progresses through a series of pages and then prompts the user to enter a number. The goal is to get the wizard to then increase by the number input at the txtCtrl box. I am having difficulty accessing the pageList list within the wizard class responsible for managing the top level aspects of the wizard. With the following code :

import wx
import wx.wizard as wiz

########################################################################


#----------------------------------------------------------------------
# Wizard Object which contains the list of wizard pages.
class DynaWiz(object):
    def __init__(self):
        wizard = wx.wizard.Wizard(None, -1, "Simple Wizard")
        self.pageList = [TitledPage(wizard, "Page 1"),
                    TitledPage(wizard, "Page 2"),
                    TitledPage(wizard, "Page 3"),
                    TitledPage(wizard, "Page 4"),
                    AddPage(wizard)]
        for i in range(len(self.pageList)-1):
            wx.wizard.WizardPageSimple.Chain(self.pageList[i],self.pageList[i+1])

        wizard.FitToPage(self.pageList[0])

        wizard.RunWizard(self.pageList[0])

        wizard.Destroy()

#----------------------------------------------------------------------
#generic wizard pages
class TitledPage(wiz.WizardPageSimple):
    def __init__(self, parent, title):
        """Constructor"""
        wiz.WizardPageSimple.__init__(self, parent)

        sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(sizer)

        title = wx.StaticText(self, -1, title)
        title.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))
        sizer.Add(title, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        sizer.Add(wx.StaticLine(self, -1), 0, wx.EXPAND|wx.ALL, 5)

#----------------------------------------------------------------------
# page used to identify number of pages to add
class AddPage(wiz.WizardPageSimple):
    def __init__(self,parent):
        self.parent = parent
        """Constructor"""
        wiz.WizardPageSimple.__init__(self, parent)


        sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(sizer)
        self.numPageAdd = wx.TextCtrl(self, -1, "")
        self.verifyButton = wx.Button(self, id=wx.ID_ANY, label = "Confirm",name = "confirm")
        self.verifyButton.Bind(wx.EVT_BUTTON, self.append_pages)

        sizer.Add(self.numPageAdd, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
        sizer.Add(self.verifyButton,0,wx.ALIGN_CENTER|wx.ALL, 5)
        sizer.Add(wx.StaticLine(self, -1), 0, wx.EXPAND|wx.ALL, 5)

    #function used to add pages to pageList inside of Wizard Object containing
    # this page
    def append_pages(self,event):
        n = int(self.numPageAdd.GetValue())
        for i in range(n):
            #Add n number of pages to wizard list "pageList" here....
            self.parent.pageList.append(TitledPage(wizard, "Added Page"))

#----------------------------------------------------------------------

if __name__ == "__main__":
    app = wx.App(False)
    dWiz = DynaWiz()
    app.MainLoop()

Using this code generated the following error message:

AttributeError: 'Wizard' object has no attribute 'pageList'

And I understand why that is, because ultimately the parent of the page is the Wizard object and not the DynaWiz object. That being said, is there a way to access the pageList list in the DynaWiz obect AND ensure that the current wizard gets reloaded from within the event in the AddPage class?


Solution

  • You could just pass the Dynawiz instance to AddPage's constructor. Then AddPage can modify pageList. See below:

    import wx
    import wx.wizard as wiz
    
    ########################################################################
    
    
    #----------------------------------------------------------------------
    # Wizard Object which contains the list of wizard pages.
    class DynaWiz(object):
        def __init__(self):
            wizard = wx.wizard.Wizard(None, -1, "Simple Wizard")
            self.pageList = [TitledPage(wizard, "Page 1"),
                        TitledPage(wizard, "Page 2"),
                        TitledPage(wizard, "Page 3"),
                        TitledPage(wizard, "Page 4"),
                        AddPage(wizard, self)]
            for i in range(len(self.pageList)-1):
                wx.wizard.WizardPageSimple.Chain(self.pageList[i],self.pageList[i+1])
    
            wizard.FitToPage(self.pageList[0])
    
            wizard.RunWizard(self.pageList[0])
    
            wizard.Destroy()
    
    #----------------------------------------------------------------------
    #generic wizard pages
    class TitledPage(wiz.WizardPageSimple):
        def __init__(self, parent, title):
            """Constructor"""
            wiz.WizardPageSimple.__init__(self, parent)
    
            sizer = wx.BoxSizer(wx.VERTICAL)
            self.SetSizer(sizer)
    
            title = wx.StaticText(self, -1, title)
            title.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))
            sizer.Add(title, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
            sizer.Add(wx.StaticLine(self, -1), 0, wx.EXPAND|wx.ALL, 5)
    
    #----------------------------------------------------------------------
    # page used to identify number of pages to add
    class AddPage(wiz.WizardPageSimple):
        def __init__(self,parent,dynawiz):
            self.parent = parent
            self.dynawiz = dynawiz
            """Constructor"""
            wiz.WizardPageSimple.__init__(self, parent)
    
    
            sizer = wx.BoxSizer(wx.VERTICAL)
            self.SetSizer(sizer)
            self.numPageAdd = wx.TextCtrl(self, -1, "")
            self.verifyButton = wx.Button(self, id=wx.ID_ANY, label = "Confirm",name = "confirm")
            self.verifyButton.Bind(wx.EVT_BUTTON, self.append_pages)
    
            sizer.Add(self.numPageAdd, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
            sizer.Add(self.verifyButton,0,wx.ALIGN_CENTER|wx.ALL, 5)
            sizer.Add(wx.StaticLine(self, -1), 0, wx.EXPAND|wx.ALL, 5)
    
        #function used to add pages to pageList inside of Wizard Object containing
        # this page
        def append_pages(self,event):
            n = int(self.numPageAdd.GetValue())
            for i in range(n):
                #Add n number of pages to wizard list "pageList" here....
                self.dynawiz.pageList.append(TitledPage(self.parent, "Added Page"))
                wx.wizard.WizardPageSimple.Chain(self.dynawiz.pageList[-2],self.dynawiz.pageList[-1])
            self.parent.FindWindowById(wx.ID_FORWARD).SetLabel("Next >")
    
    #----------------------------------------------------------------------
    
    if __name__ == "__main__":
        app = wx.App(False)
        dWiz = DynaWiz()
        app.MainLoop()