I am doing simple file I/O. I have a Directory trait SaveDir
and a File trait SaveFile
. How to i access the directory path entered in the GUI, or the default? e.g., i would like to print it out, as in the following example.
Do i use get_value, e.g., SaveDir.get_value
? I can't figure it out...
Once I can access the value, I want to make a path string to i can open the file for writing, e.g. self.writefile = open(path,'w')
.
THanks, Cosmo
class ControlPanel(HasTraits):
SaveFile = Str("MyDAta")
SaveDir = Directory("C:/My Documents/Data")
view = View(Item('SaveFile',label='Save Filename',style='simple'),Item('SaveDir',label='Data Directory', style='simple'))
print SaveDir
You need to create an instance of the class, then call configure_traits on it. You could then inspect its SaveDir trait. Typically you would create a change notification method and/or a button.
Please see the materials referenced here: https://support.enthought.com/entries/22878645-Introductory-materials-for-Traits-and-Traits-UI
Then, I suggest that you start with a class that just has strings and integers, and learn how to use these. You will then be able to extend this to Directory if you like (though for real-world programs, the Directory trait is fairly inflexible, and other ways are often preferable.)
Update: You can find many useful examples in the Examples/traitsui-4.2.0
subdirectory of your Canopy User Python directory.
Update 2: For more useful file selection dialogs, see the pyface package, in particular: https://github.com/enthought/pyface/blob/master/pyface/i_file_dialog.py