wxPython return 从向导到调用框架的值

wxPython return value from Wizard to calling Frame

我的问题如下:

我正在设计一个向导,用于构造要添加到程序调用框架中的对象列表的对象。在向导结束时,我想将新创建的对象传递回调用框架以插入到列表中。为了在抽象的基础上模拟这个基本功能,我构建了以下按比例缩小的应用程序:

mainframe.py

import wx
import wiz_test
class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,title="Main")
        panel = wx.Panel(self)
        callButton = wx.Button(panel, label = "Call Wizard")
        callButton.Bind(wx.EVT_BUTTON,self.launchWizard)
        self.Show()
    def launchWizard(self,event):
        wiz = wiz_test.WizObj(self)
        a = 0
        if wiz == wx.wizard.EVT_WIZARD_FINISHED:
            a = wiz.answer
        print a

if __name__ == '__main__':
    app = wx.App(False)
    frame = MainFrame()
    app.MainLoop()

wiz_test.py

import wx
import wx.wizard as wiz

class WizPage(wiz.WizardPageSimple):
    def __init__(self, parent):
        self.answer = 3
        wiz.WizardPageSimple.__init__(self, parent)

        sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(sizer)

        title = wx.StaticText(self, -1, "Wizard Page")
        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)


class WizObj(object):
    def __init__(self,parent):
        wizard = wx.wizard.Wizard(None, -1, "Simple Wizard")
        page1 = WizPage(wizard)
        wizard.FitToPage(page1)
        wizard.RunWizard(page1)
        wizard.Destroy()

if __name__ == "__main__":
    app = wx.App(False)
    main()
    app.MainLoop()

这个小例子的最终目的是让MainFrame实例在触发wx.wizard.EVT_WIZARD_FINISHED事件时输出WizObj实例的.answer成员变量派生的值'3'。但是,由于当前代码仅 returns '0',此时显然无法正常工作。我是以正确的方式接近这个吗?我应该改为绑定 EVT_WIZARD_FINISHED 事件吗?如果是这样,我如何从大型机访问它?

我能够通过使用 wxPython 库中的 "pubsub" 功能解决这个问题。具体来说,我在调用框架中的向导实例化之前立即添加了一个 pub.subscribe() 实例。在向导内部,我在销毁向导之前通过 pub.sendMessage() 传递了值。重要的是要注意,必须指定传递值才能使 pubsub 发送有效。

以下代码是现在可以运行的原始代码的修改版本。

MainFrame.py

import wx
import wiz_test
from wx.lib.pubsub import pub

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,title="Main")
        panel = wx.Panel(self)
        callButton = wx.Button(panel, label = "Call Wizard")
        callButton.Bind(wx.EVT_BUTTON,self.launchWizard)
        self.Show()

    def catch_stuff(self,a):
        print a

    def launchWizard(self,event):
        pub.subscribe(self.catch_stuff,'valPass')
        wiz = wiz_test.WizObj(self,a)



if __name__ == '__main__':
    app = wx.App(False)
    frame = MainFrame()
    app.MainLoop()

wiz_test.py

import wx
import wx.wizard as wiz
from wx.lib.pubsub import pub

class WizPage(wiz.WizardPageSimple):
    def __init__(self, parent):
        self.answer = 3
        wiz.WizardPageSimple.__init__(self, parent)

        sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(sizer)

        title = wx.StaticText(self, -1, "Wizard Page")
        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)

#----------------------------------------------------------------------
class WizObj(object):
    def __init__(self,parent,a):
        wizard = wx.wizard.Wizard(None, -1, "Simple Wizard")
        page1 = WizPage(wizard)
        wizard.FitToPage(page1)
        wizard.RunWizard(page1)
        pub.sendMessage('valPass',a = page1.answer)
        wizard.Destroy()

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    main()
    app.MainLoop()

结果是控制台打印从调用的向导中检索到的值 3