简体   繁体   中英

wxPython: make variables available to an imported class?

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?

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM