0

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  
DanG
  • 306
  • 7
  • 15

2 Answers2

0

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

Jonathan March
  • 5,800
  • 2
  • 14
  • 16
  • so does invoking the method create an instance, as in the code in my answer below? Can you explain what is going on? – DanG Nov 12 '13 at 18:01
  • Dan, not sure I understand your question. You didn't get that output from only the code that you show. Presumably you ran it with something like this: "c = ControlPanel(); c.configure_traits()". So the instance is created by the "ControlPanel()" call and saved as "c", and when you call any method on c, then c is passed to that method as "self". – Jonathan March Nov 13 '13 at 02:25
  • A good source for study: http://www.amazon.com/Python-Essential-Reference-4th-Edition/dp/0672329786 – Jonathan March Nov 13 '13 at 02:29
0

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

DanG
  • 306
  • 7
  • 15
  • precisely, well done. Except stylistically, see http://www.python.org/dev/peps/pep-0008/#method-names-and-instance-variables – Jonathan March Nov 12 '13 at 01:49