1

私は TraitsUI 内で FileDialog クラスを使用しています。これは非常にうまく機能しますが、私の人生を除いて、ダイアログが使用するためにデフォルトのディレクトリ を渡す方法を理解できませんでした。

理想的には、ツリーの最上部以外のローカル ファイル システムのポイントでダイアログ ボックスが開きます...

初心者からの洞察や方向性は非常に感謝しています。

次のように、基本コードはかなり一般的/標準的です。

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
4

3 に答える 3

2

TraitsUI FileDialog を使用しないことをお勧めします。pyface.api.FileDialog (ツールキット固有。API については、 https://github.com/enthought/pyface/blob/master/pyface/i_file_dialog.pyを参照) を使用すると、よりうまくいくと思います。

于 2014-10-02T21:49:45.293 に答える
0

おそらく手遅れですが、例を次に示します。

#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
于 2015-07-20T22:09:35.973 に答える