简体   繁体   English

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

[英]wxPython: make variables available to an imported class?

I have written a wxPython GUI which assigns some variables upon a button click. 我编写了一个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")

I then run execfile, and the executed file imports and runs a class from another file. 然后,我运行execfile,执行的文件导入并运行另一个文件中的类。

How can I make the variables I've defined through my GUI available to the imported class? 如何使通过GUI定义的变量可用于导入的类?

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. 这是foo.py文件。

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. 如果运行wxPython脚本,它将执行foo.py ,该脚本可以访问wxPython脚本中的局部变量和全局变量。 You won't be able to run foo.py by itself though. 但是,您将无法单独运行foo.py。 See the following: 请参阅以下内容:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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