2014-10-02 86 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