Question

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  
Was it helpful?

Solution

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

OTHER TIPS

Following @Jon (I think this is what he meant), it is possible to access the directory as a string inside a method in the class. In particular, inside the method of a button works.

I am not clear about this, but it seams that invoking the method also creates the instance self of the the class ControlPanel. Is this correct?

class ControlPanel(HasTraits): 
    SaveFile = Str("MyDAta")
    SaveDir = Directory("C:/My Documents/Data")
    start = Button("Start Measurements")


    view = View(Item('SaveFile',label='Save Filename',style='simple'),
                Item('SaveDir',label='Data Directory', style='simple'), 
                UItem('start',style='custom'))

    def _start_fired(self):
        print self.SaveDir

Prints:

C:/My Documents/Data

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top