wxPython:使变量可用于导入的类?

时间:2014-07-10 20:42:04

标签: python class wxpython

我编写了一个wxPython GUI,可以在按下按钮时分配一些变量。

def OnGo(self, event):
    inputdatadirectory = self.txtbx1.GetValue() 
    outputsavedirectory = self.txtbx2.GetValue()
    mcadata = self.txtbx3.GetValue()
    current_dir = os.getcwd()
    execfile(current_dir+"\\aEDXD.py")

然后我运行execfile,执行的文件从另一个文件导入并运行一个类。

如何通过我的GUI定义的变量可用于导入的类?

1 个答案:

答案 0 :(得分:0)

是的,你可以,虽然它可能很难调试。这是一个愚蠢的例子:

import wx

########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Test")
        panel = wx.Panel(self)
        btn = wx.Button(panel, label="Go")
        btn.Bind(wx.EVT_BUTTON, self.onGo)
        self.Show()

    #----------------------------------------------------------------------
    def onGo(self, event):
        """"""
        foobar = "This is a test!"
        execfile("foo.py")

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

这是 foo.py 文件。

print foobar

如果你运行wxPython脚本,它将执行 foo.py ,它可以访问wxPython脚本中的locals和globals。但你不能自己运行foo.py.请参阅以下内容: