【问题标题】:wxPython dynamically add pages to wizardwxPython 动态添加页面到向导
【发布时间】:2016-12-04 16:33:44
【问题描述】:

我一直致力于开发一个基于 wxPython 的向导,我希望它能够根据向导本身提供的输入动态增加大小。该向导会浏览一系列页面,然后提示用户输入一个数字。目标是让向导增加在 txtCtrl 框中输入的数字。我无法访问负责管理向导顶级方面的向导类中的 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)]
        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' 对象没有属性 'pageList'

我理解为什么会这样,因为最终页面的父级是 Wizard 对象而不是 DynaWiz 对象。话虽如此,有没有办法访问 DynaWiz 对象中的 pageList 列表并确保从 AddPage 类的事件中重新加载当前向导?

【问题讨论】:

    标签: python wxpython wizard dynamically-generated


    【解决方案1】:

    您可以将 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()
    

    【讨论】:

    • 你知道吗,在我的电池“令人难以置信的沮丧尝试让这个工作”中,我尝试做这样的事情,但我试图在接收班的错误点接受通行证。这次真是万分感谢!太完美了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    相关资源
    最近更新 更多