DunkM
DunkM

Reputation: 11

Adding default file directory to FileDialog in Traits

I am using the FileDialog class within TraitsUI, which works pretty well, except for the life of me, I have not been able to figure out how to pass a default directory, for the dialogue to use.

Ideally, the dialogue box would open at a point in the local file system other than the top of the tree...

Any insight or direction very gratefully appreciated from a newbie.

Base code pretty generic/standard as follows.

demo_id = 'traitsui.demo.standard_editors.file_dialog.file_info'

class FileDialog ( HasTraits ):

    # The name of the selected file:
    file_name = File
    # The button used to display the file dialog:
    open = Button( 'Open...' )

    #-- Traits View Definitions ------------------------------------------------

    view = View(
        HGroup(
            Item( 'open', show_label = False ),
            '_',
            Item( 'file_name', style = 'readonly', springy = True )
        ),
        width = 0.5
    )

    #-- Traits Event Handlers --------------------------------------------------

    def _open_changed ( self ):
        """ Handles the user clicking the 'Open...' button.
        """
        file_name = open_file( extensions = FileInfo(), id = demo_id )
        if file_name != '':
            self.file_name = file_name

Upvotes: 1

Views: 970

Answers (3)

Gault Drakkor
Gault Drakkor

Reputation: 111

Probably too late but here is an example:

#other traits imports
from pyface.api import FileDialog
class Something(HasTraits):
    txt_file_name = File
    openTxt = Button('Open...')
    traits_view = View( 
        VGroup( 
            HGroup(
              Item( 'openTxt', show_label = False ),
              '_',
              Item( 'txt_file_name', style = 'readonly', width = 200 ),
            ),
        )
        )
    def _openTxt_fired(self):
        """ Handles the user clicking the 'Open...' button.
        """
        extns = ['*.txt',]#seems to handle only one extension...
        wildcard='|'.join(extns)

        dialog = FileDialog(title='Select text file',
            action='open', wildcard=wildcard,
             default_path = self.txt_file_name)
        if dialog.open() == OK:
            self.txt_file_name = dialog.path
            self.openTxtFile(dialog.path)     
    def openTxtFile(self, path):
        'do something'
        print path

Upvotes: 0

OYRM
OYRM

Reputation: 1415

this is an easy one. Basically, when you execute the open_file method, you have the opportunity to pass to it traits definitions which will in turn be passed to the OpenFileDialog object created within that convenience method. You are already doing so with the following

open_file( extensions = FileInfo(), id = demo_id )

Simply add to that a definition for "file_name" and you're set.

open_file( file_name="/foo/bar" extensions = FileInfo(), id = demo_id )

reading from the source of traitui.file_dialog.py, you can see the mechanism by which file_name gets passed from open_file to OpenFileDialog, the Handler responsible for representing the file dialog itself.

def open_file ( **traits ):
  ...
  fd = OpenFileDialog( **traits )
  ...

class OpenFileDialog ( Handler ):
  ...
  # The starting and current file path:
  file_name = File
  ...

Upvotes: 1

Jonathan March
Jonathan March

Reputation: 5810

I suggest not using the TraitsUI FileDialog. I think you'll do better with pyface.api.FileDialog (toolkit-specific; for the API, see https://github.com/enthought/pyface/blob/master/pyface/i_file_dialog.py).

Upvotes: 2

Related Questions