2016-07-29 93 views
0

我一直在開發一個基於wxPython的嚮導,我希望能夠根據嚮導本身提供的輸入動態增加大小。該向導通過一系列頁面前進,然後提示用戶輸入一個數字。目標是通過txtCtrl框中的數字輸入獲取嚮導。我無法訪問負責管理嚮導頂級方面的嚮導類中的pageList列表。用下面的代碼:wxPython動態添加頁面到嚮導

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() 

使用此代碼生成以下錯誤消息:

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

而且我明白這是爲什麼,因爲最終頁面的父母是嚮導對象,而不是DynaWiz目的。這就是說,有沒有辦法訪問DynaWiz obect中的pageList列表,並確保當前嚮導從AddPage類中的事件中重新加載?

回答

0

您可以將Dynawiz實例傳遞給AddPage的構造函數。然後AddPage可以修改pageList。請看下圖:

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() 
+0

你知道嗎,我想在我的電池做這樣的事情早「令人難以置信的沮喪試圖得到這個工作,」但我想接受在錯誤的點在接收類的通。這次真是萬分感謝!這十分完美! –