0

I have written a wxPython GUI which assigns some variables upon a button click.

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")

I then run execfile, and the executed file imports and runs a class from another file.

How can I make the variables I've defined through my GUI available to the imported class?

user2963623
  • 2,267
  • 1
  • 14
  • 25

1 Answers1

0

Yes you can, although it will probably be difficult to debug. Here is a silly example:

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()

And here is the foo.py file.

print foobar

If you run the wxPython script, it will execute foo.py which has access to the locals and globals in the wxPython script. You won't be able to run foo.py by itself though. See the following:

Community
  • 1
  • 1
Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88