0

I created a variable (snowDIR) via GetPath inside a wx.DirDialog box and I would like to use snowDIR outside the function. There is a sample of my code:

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

any help would be greatly appreciated since I've search the net without lock and I'm getting out of time.

1 Answer 1

1

Just assign it to self.snowDir and you can access it from your object and from anything that has access to it! 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()

In the same scope - i.e. as part of the same object:

def OnDoitClick(self, event):
    """ Do the action that uses snowDir """
    self.MOD01_dir = os.path.join(self.snowDIR,'MOD01')
    ....

Outside the scope: *Assuming that the above code is a part of the class definition of *MyFrameClass and that your App uses TheFrame = MyFrameClass(...) at the same level as the above you can use:

if hasattr(TheFrame, snowDIR):
   print 'snowDIR', TheFrame.snowDIR
else:
   print 'User did not set snowDIR'
1
  • I have changed all the snowDIR for self.snowDIR with this result: self.MOD01_dir = os.path.join(self.snowDIR,'MOD01') AttributeError: 'PanelSNOW' object has no attribute 'snowDIR' I then try to assign self.snowDIR to a new value, with about the same result: snowDIR2 = self.snowDIR AttributeError: 'PanelSNOW' object has no attribute 'snowDIR'
    – GuillaumeD
    Aug 9, 2013 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.