Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将默认文件目录添加到中的FileDialog_Python_File_Enthought_Traitsui - Fatal编程技术网

Python 将默认文件目录添加到中的FileDialog

Python 将默认文件目录添加到中的FileDialog,python,file,enthought,traitsui,Python,File,Enthought,Traitsui,我在TraitsUI中使用FileDialog类,它工作得很好,除了我的生活之外,我还没有弄清楚如何传递一个默认目录,以便对话使用 理想情况下,对话框将在本地文件系统中的某个点打开,而不是在树的顶部 新手非常感激的任何见解或指导 基本代码非常通用/标准,如下所示 demo_id = 'traitsui.demo.standard_editors.file_dialog.file_info' class FileDialog ( HasTraits ): # The name of t

我在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

我建议不要使用TraitsUI文件对话框。我认为使用pyface.api.FileDialog(特定于工具包;有关api,请参阅)会做得更好。

这是一个简单的方法。基本上,当您执行
open_file
方法时,您有机会将特性定义传递给它,这些特性定义将依次传递给在该方便方法中创建的
OpenFileDialog
对象。您已经在使用以下工具执行此操作

打开文件(扩展名=FileInfo(),id=demo\u id)

只需添加一个“文件名”的定义,就可以了

打开文件(文件名=“/foo/bar”扩展名=FileInfo(),id=demo\u id)

traitui.file\u dialog.py
的源代码中,您可以看到文件名从
open\u file
传递到
OpenFileDialog
的机制,该处理程序负责表示文件对话框本身

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

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

可能太晚了,但这里有一个例子:

#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

这是对API更有用的描述