Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在wx python中调用在事件函数中创建的变量_Python_Variables_Wxpython - Fatal编程技术网

在wx python中调用在事件函数中创建的变量

在wx python中调用在事件函数中创建的变量,python,variables,wxpython,Python,Variables,Wxpython,我在wx.dir对话框中通过GetPath创建了一个变量(snowDIR),我想在函数外部使用snowDIR。下面是我的代码示例: for file in os.listdir(snowDIR): if fnmatch.fnmatch(file, '*.hdf'): if file[9:16] == a: inputhdf = (snowDIR + '\\' + file)

我在wx.dir对话框中通过GetPath创建了一个变量(snowDIR),我想在函数外部使用snowDIR。下面是我的代码示例:

for file in os.listdir(snowDIR):
            if fnmatch.fnmatch(file, '*.hdf'): 
                if file[9:16] == a: 
                    inputhdf =  (snowDIR + '\\' + file) 
                    print 'input hdf is: ', inputhdf
                    tmod = 1

def OnDownload(self, e):
    modisPathFile = 'MODIS_data_directory_path.txt'
    dlg = wx.DirDialog(self, "Choose a directory:",
                       style=wx.DD_DEFAULT_STYLE
                       #| wx.DD_DIR_MUST_EXIST
                       #| wx.DD_CHANGE_DIR
                       )

    if dlg.ShowModal() == wx.ID_OK:
        print "You chose %s" % dlg.GetPath()
        snowDIR = dlg.GetPath()
        print 'snowDIR : ', snowDIR
        dlg.Destroy()

more code ....

return snowDIR

如果有任何帮助,我将不胜感激,因为我已经在没有锁的情况下搜索了网络,而且时间不多了。

只需将其分配给
self.snowDir
,您就可以从您的对象和任何可以访问它的对象访问它!e、 g:

def OnDownload(self, e):
    modisPathFile = 'MODIS_data_directory_path.txt'
    dlg = wx.DirDialog(self, "Choose a directory:",
                       style=wx.DD_DEFAULT_STYLE
                       #| wx.DD_DIR_MUST_EXIST
                       #| wx.DD_CHANGE_DIR
                       )

    if dlg.ShowModal() == wx.ID_OK:
        print "You chose %s" % dlg.GetPath()
        self.snowDIR = dlg.GetPath()
        print 'snowDIR : ', self.snowDIR
        dlg.Destroy()
在同一范围内-即作为同一对象的一部分:

超出范围:*假设上述代码是*
MyFrameClass
类定义的一部分,并且您的应用程序使用的
TheFrame=MyFrameClass(…)
级别与您可以使用的级别相同:


我已经更改了self.snowDIR的所有snowDIR,结果是:self.MOD01_dir=os.path.join(self.snowDIR,'MOD01')AttributeError:'PanelSNOW'对象没有属性'snowDIR',然后我尝试将self.snowDIR分配给一个新值,结果大致相同:snowDIR2=self.snowDIR AttributeError:“PanelSNOW”对象没有属性“snowDIR”
def OnDoitClick(self, event):
    """ Do the action that uses snowDir """
    self.MOD01_dir = os.path.join(self.snowDIR,'MOD01')
    ....
if hasattr(TheFrame, snowDIR):
   print 'snowDIR', TheFrame.snowDIR
else:
   print 'User did not set snowDIR'