2013-02-04 49 views
3

如果我通过方法Gtk.FileChooserWidget.set_current_folder()设置当前文件夹中,我第一次打开文件选择器,它会视作为论据set_current_folder()如何设置Gtk.FileChooserWidget的默认开放路径?

的位置,但是,如果我选择一个文件时,我再次打开文件选择器,它会在“most_recent_used_files”上打开。

我想它会打开最后选定的文件的文件夹路径。

怎么办?

谢谢。

+0

如果你使用set_current_folder每次文件被打开? –

+0

它不起作用。从这个角度来看,这个小部件有一个非常奇怪的行为。也许这是一个错误。 – Irr

回答

3

从文档:

文件选择的文档的旧版本使用在各种情况下gtk_file_chooser_set_current_folder(),以让应用程序的意图提出一个合理的默认文件夹建议。这不再被认为是一个好策略,因为现在文件选择器能够自行提出好的建议。一般来说,只有当文件选择器适合使用gtk_file_chooser_set_filename()时,才应使文件选择器显示特定的文件夹 - 即当您正在执行文件/另存为命令并且您已将某个文件保存在某处时。

您可能会或可能不喜欢这种行为的原因。如果您对它的出现感到好奇,请参阅邮件列表中的File chooser recent-files以及GNOME wiki上的Help the user choose a place to put a new file

0

设置当前文件夹中,每次对我的作品,但它是一个有点棘手。我使用的是Gtk 3.14和Python 2.7。

你必须重置目录之前得到的文件名,或者它丢失,并且当前目录可能是无,所以你必须以检查。

此代码是Debian的杰西测试和Windows 7

import os.path as osp 

from gi.repository import Gtk 

class FileDialog(Gtk.FileChooserDialog): 
    def __init__(self, parent, title): 
     Gtk.FileChooserDialog.__init__(self, title, parent) 
     self.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL) 
     self.add_button(Gtk.STOCK_OPEN, Gtk.ResponseType.OK) 

     self.set_current_folder(osp.abspath('.')) 

    def __call__(self): 
     resp = self.run() 
     self.hide() 

     fname = self.get_filename() 

     d = self.get_current_folder() 
     if d: 
      self.set_current_folder(d) 

     if resp == Gtk.ResponseType.OK: 
      return fname 
     else: 
      return None 

class TheApp(Gtk.Window): 
    def on_clicked(self, w, dlg): 
     fname = dlg() 
     print fname if fname else 'canceled' 

    def __init__(self): 
     Gtk.Window.__init__(self) 

     self.connect('delete_event', Gtk.main_quit) 
     self.set_resizable(False) 

     dlg = FileDialog(self, 'Your File Dialog, Sir.') 
     btn = Gtk.Button.new_with_label('click here') 
     btn.connect('clicked', self.on_clicked, dlg) 

     self.add(btn) 
     btn.show() 

if __name__ == '__main__': 
    app = TheApp() 
    app.show() 
    Gtk.main()