[英]wxPython: make variables available to an imported class?
我编写了一个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定义的变量可用于导入的类?
是的,您可以,尽管可能很难调试。 这是一个愚蠢的例子:
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脚本中的局部变量和全局变量。 但是,您将无法单独运行foo.py。 请参阅以下内容:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.