2014-10-02 81 views
0

我在TraitsUI中使用FileDialog类,它工作得很好,除了我的生活,我还没有能够弄清楚如何通过默认目录,for要使用的对话。在Traits中添加默认文件目录到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 

回答

1

我建议使用TraitsUI FileDialog的。我想你会用pyface.api.FileDialog做得更好(特定于工具包;对于API,请参阅https://github.com/enthought/pyface/blob/master/pyface/i_file_dialog.py)。

+1

这是API https://svn.enthought.com/enthought/wiki/FileDialogDemo – aestrivex 2015-02-04 19:49:42

+0

感谢更有用的描述,良好的点https://github.com/enthought/pyface/issues/119 – 2015-02-04 19:59:01

0

这是一个容易的。基本上,当您执行open_file方法时,您有机会传递给它的特质定义,然后将其传递给在该便捷方法中创建的OpenFileDialog对象。您已经与下列

OPEN_FILE这样做(扩展= FileInfo的(),ID = demo_id)

只需添加为 “FILE_NAME” 一个定义,你设置。

OPEN_FILE(FILE_NAME = “/富/栏” 扩展= FileInfo的(),ID = demo_id)

traitui.file_dialog.py的源读取,可以看到通过该file_name中会从通过的机构open_fileOpenFileDialog,处理程序负责表示文件对话框本身。

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

class OpenFileDialog (Handler): 
    ... 
    # The starting and current file path: 
    file_name = File 
    ... 
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