Dynamic wxChoice with wxPyWizardPage Demo Code

Here is a copy of some demo code that I wrote to implement Martin's suggestion for implementing a dynamic wxChoice control in a Wizard.
http://aspn.activestate.com/ASPN/Mail/Message/wxPython-users/1560670
(Thanks for the tip Martin!)

Please feel free to forward any comments and/or suggestions on cleaner or better ways to implement this.

Best Regards,

Darren

···

---------------------------------------------------------------------

from wxPython.wx import *
from wxPython.wizard import *

def makePageTitle(wizPg, title):
      sizer = wxBoxSizer(wxVERTICAL)
      wizPg.SetSizer(sizer)
      title = wxStaticText(wizPg, -1, title)
      title.SetFont(wxFont(18, wxSWISS, wxNORMAL, wxBOLD))
      sizer.AddWindow(title, 0, wxALIGN_CENTRE|wxALL, 5)
      sizer.AddWindow(wxStaticLine(wizPg, -1), 0, wxEXPAND|wxALL, 5)
      return sizer

class FirstChoicePage1(wxPyWizardPage):
     def __init__(self, parent, title):
         wxPyWizardPage.__init__(self, parent)
         self.next = self.prev = None
         self.sizer = makePageTitle(self, title)
         self.FirstChoice = ''

         self.ch = wxChoice(self, 40, (80, 50), choices = ['Happy Text', 'Sad Text'])
         EVT_CHOICE(self, 40, self.EvtChoice)
         self.sizer.Add(self.ch, 0, wxALL, 5)

     def EvtChoice(self, event):
         self.FirstChoice = event.GetString()

     def SetNext(self, next):
         self.next = next

     def SetPrev(self, prev):
         self.prev = prev

     # Classes derived from wxPyWizardPanel must override
     # GetNext and GetPrev, and may also override GetBitmap
     # as well as all those methods overridable by
     # wxPyWindow.

     def GetNext(self):
         """ Dynamically create a wxChoice control for the next page based on the value in self.FirstChoice"""
         next = self.next
         if self.FirstChoice == 'Happy Text':
             next.ch = wxChoice(next, 40, (80, 50), choices = ['Happy Smiles', 'Happy Laughs'])
             self.sizer.Add(next.ch, 0, wxALL, 5)
         else:
             if self.FirstChoice == 'Sad Text':
                 next.ch = wxChoice(next, 40, (80, 50), choices = ['Sad Frowns', 'Sad Tears'])
                 self.sizer.Add(next.ch, 0, wxALL, 5)
         return next

     def GetPrev(self):
         return self.prev

class DynamicPage2(wxPyWizardPage):
     def __init__(self, parent, title):
         wxPyWizardPage.__init__(self, parent)
         self.next = self.prev = None
         self.sizer = makePageTitle(self, title)
         self.SecondChoice = ''

         EVT_CHOICE(self, 40, self.EvtChoice)

     def EvtChoice(self, event):
         self.SecondChoice = event.GetString()

     def SetNext(self, next):
         self.next = next

     def SetPrev(self, prev):
         self.prev = prev

     def GetNext(self):
         return self.next

     def GetPrev(self):
         return self.prev

app = wxPySimpleApp()
wizard = wxWizard(None, -1, "Simple Wizard")
page1 = FirstChoicePage1(wizard, "Make a Selection")
page2 = DynamicPage2(wizard, "Are Selections Correct?")
# Set the page order
page1.SetNext(page2)
page2.SetPrev(page1)
wizard.FitToPage(page1)
wizard.RunWizard(page1)
wizard.Destroy()
app.MainLoop()