Wxpython向导wx.adv.Wizard组件

 

前言

  在界面程序主界面显示前,向导界面会引导用户初始化和配置其他数据,wxpython提供了wx.adv.Wizard,可参考:

  文档:https://wxpython.org/Phoenix/docs/html/wx.adv.Wizard.html

 

Demo实现

import wx
import wx.adv

class configWizard(wx.adv.Wizard):
    def __init__(self, addNew = False):
        super(configWizard, self).__init__(None, -1, "Configuration Wizard")

        self.Bind(wx.adv.EVT_WIZARD_PAGE_CHANGED, self.OnPageChanged)
        self.Bind(wx.adv.EVT_WIZARD_PAGE_CHANGING, self.OnPageChanging)

        self.firstInfoPage = Info1Page(self,'title1')
        self.machineSelectPage = Info2Page(self,'title2')

        wx.adv.WizardPageSimple.Chain(self.firstInfoPage, self.machineSelectPage)

        # self.GetPageAreaSizer().Add(self.firstInfoPage)
        self.RunWizard(self.firstInfoPage)
        self.Destroy()

    def OnPageChanging(self, e):
        print(e.GetPage())
        # e.GetPage().StoreData()

    def OnPageChanged(self, e):
        if e.GetPage().AllowNext():
            self.FindWindowById(wx.ID_FORWARD).Enable()
        else:
            self.FindWindowById(wx.ID_FORWARD).Disable()
        if e.GetPage().AllowBack():
            self.FindWindowById(wx.ID_BACKWARD).Enable()
        else:
            self.FindWindowById(wx.ID_BACKWARD).Disable()


class Info1Page(wx.adv.WizardPageSimple):
    def __init__(self, parent, title):
        wx.adv.WizardPageSimple.__init__(self, parent)
        sizer = wx.GridBagSizer(5, 5)
        self.sizer = sizer
        self.SetSizer(sizer)

        title = wx.StaticText(self, -1, title)
        title.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))
        sizer.Add(title, pos=(0, 0), span=(1, 2), flag=wx.ALIGN_CENTRE | wx.ALL)
        sizer.Add(wx.StaticLine(self, -1), pos=(1, 0), span=(1, 2), flag=wx.EXPAND | wx.ALL)
        sizer.AddGrowableCol(1)

    def  AllowNext(self):
        print(1)
        return True

    def AllowBack(self):
        print(2)
        return False

class Info2Page(wx.adv.WizardPageSimple):
    def __init__(self, parent, title):
        wx.adv.WizardPageSimple.__init__(self, parent)
        sizer = wx.GridBagSizer(5, 5)
        self.sizer = sizer
        self.SetSizer(sizer)

        title = wx.StaticText(self, -1, title)
        title.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))
        sizer.Add(title, pos=(0, 0), span=(1, 2), flag=wx.ALIGN_CENTRE | wx.ALL)
        sizer.Add(wx.StaticLine(self, -1), pos=(1, 0), span=(1, 2), flag=wx.EXPAND | wx.ALL)
        sizer.AddGrowableCol(1)

    def  AllowNext(self):
        print(1)
        return True

    def AllowBack(self):
        print(2)
        return True


class Example(wx.Frame):
    def __init__(self, *args, **kw):
        super(Example, self).__init__(*args, **kw)
        configWizard()
        self.InitButton()
        self.SetSize((300, 200))
        self.SetTitle('wx.Gauge')
        self.Centre()
        self.Show(True)
    def InitButton(self):
        pnl = wx.Panel(self)
        self.btn1 = wx.Button(pnl, label="Start")
        self.Bind(wx.EVT_BUTTON, self.OnStart, self.btn1)
    def OnStart(self, e):
        pass

def main():
    ex = wx.App()
    Example(None)
    ex.MainLoop()


if __name__ == '__main__':
    main()

相关代码详解

继承向导类

每一个向导page类都要继承wx.adv.WizardPageSimple,其中有两个函数必须实现:

  • AllowNext
  • AllowBack

如果此向导界面页面后还有其他向导界面则AllowNext为True,反之为False,AllowBack表示此页面可以后退,但第一个向导界面不可设置为True。

链接多个向导界面

使用wx.adv.WizardPageSimple.Chain()可以将两个向导page前后关联起来,如果有多个page,同样如此。

事件

当向导界面发生改变时(切换界面,点击finsh)会触发事件,提供了如下事件:

 

当界面内容正在更改,可通过事件参数获取到正在更改的界面,然后调用其对象(函数)对数据进行处理。

 

posted @ 2020-12-17 23:05  -零  阅读(639)  评论(0编辑  收藏  举报