By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,939 Members | 1,559 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

wx.Wizard creates pages using eval() in a List comprehension

bartonc
Expert 5K+
P: 6,596
Examples of wx.Wizard that I found used explicit names for the pages.
Using eval() in a list comprehension, I am able to go through the imported modules by name and call create() in order to get a page instance. Neat trick, I think...
Expand|Select|Wrap|Line Numbers
  1. #Boa:Wizard:Wizard1
  2.  
  3. import wx
  4. import wx.wizard
  5.  
  6. import SetupWizardPage1
  7. import SetupWizardPage2
  8.  
  9. nSetupWizardPages = 2
  10.  
  11. def create(parent):
  12.     return Wizard1(parent)
  13.  
  14. [wxID_WIZARD1] = [wx.NewId() for _init_ctrls in range(1)]
  15.  
  16. class Wizard1(wx.wizard.Wizard):
  17.     def _init_ctrls(self, prnt):
  18.         # generated method, don't edit
  19.         wx.wizard.Wizard.__init__(self, bitmap=wx.NullBitmap, id=wxID_WIZARD1, parent=prnt,
  20.               pos=wx.Point(459, 285), title='HETAP Data Collection Setup')
  21.  
  22.     def __init__(self, parent):
  23.         self._init_ctrls(parent)
  24.  
  25.     ## the old way
  26. ##        page1 = SetupWizardPage1.ParkListPage(self)
  27. ##        self.page1 = page1
  28. ##        page2 = SetupWizardPage2.TrailListPage(self)
  29.  
  30.     ## I had to add create() to the SimpleWizardPage generated by Boa
  31.         self.pages = pages = [eval("SetupWizardPage%d.create(self)" %i)
  32.                               for i in range(1, nSetupWizardPages + 1)]
  33.  
  34.         self.FitToPage(pages[0])
  35.  
  36.     ## Once they're in a list, chaining becomes a snap
  37.         for i in range(nSetupWizardPages - 1):
  38.             # Use the convenience Chain function to connect the pages in a loop
  39.             wx.wizard.WizardPageSimple_Chain(pages[i], pages[i + 1])
  40.  
  41.  
  42.         self.GetPageAreaSizer().Add(pages[0])
  43.  
  44.     def GetFirstPage(self):
  45.         """I make the wizard responsible for creating the pages, so the caller must
  46.            be able to get the first page for the Wizard.Run() argument"""
  47.         return self.pages[0]
Dec 20 '06 #1
Share this Article
Share on Google+