[英]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.