2016-07-29 2 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에서 pageList 목록에 액세스하고 현재 마법사가 AddPage 클래스의 이벤트에서 다시로드되도록하는 방법이 있습니까?

답변

0

AddPage의 생성자에 Dynawiz 인스턴스를 전달하면됩니다. 그런 다음 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

당신은 내가 "이 작업을 얻기 위해 믿을 수 없을만큼 좌절 시도"내 배터리에 앞서이 같은 일을 시도, 무엇을 알고 있지만 내가받는 클래스의 잘못된 지점에서 패스를 받아 들일려고 . 감사합니다! 이거 완벽 해! –

관련 문제