The Behavior
Everyone that has used the GTK file chooser since the horribly stupid behavior of setting it to the recent used list knows how stupid that is. No one uses that method of selecting a file it’s just too confusing.
The Fix
-
Get the users home folder in the init and save it to self.current_folder
-
Set the current folder in the file chooser to the self.current_folder
-
When a file is choosen save the current folder to self.current_folder
The Code
#!/usr/bin/python import gtk import os class anything(gtk.Window): def __init__(self): super(anything, self).__init__() self.set_title("GTK File Chooser") self.set_size_request(250, 250) self.set_position(gtk.WIN_POS_CENTER) self.connect("destroy", gtk.main_quit) # 1 get the users home directory self.current_folder = os.path.expanduser('~') # build a simple menu mb = gtk.MenuBar() menu = gtk.Menu() # create menu items filem = gtk.MenuItem("File") filem.set_submenu(menu) fopen = gtk.MenuItem("Open") fopen.connect("activate", self.file_open, "File Open") menu.append(fopen) exit = gtk.MenuItem("Exit") exit.connect("activate", gtk.main_quit) menu.append(exit) mb.append(filem) vbox = gtk.VBox(False, 2) vbox.pack_start(mb, False, False, 0) self.add(vbox) self.show_all() def file_open(self, item, data=None): print data self.fcd = gtk.FileChooserDialog("Open...", None, gtk.FILE_CHOOSER_ACTION_OPEN, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)) # 2 set the current folder in the file chooser self.fcd.set_current_folder(self.current_folder) self.response = self.fcd.run() if self.response == gtk.RESPONSE_OK: print "Selected filepath: %s" % self.fcd.get_filename() # 3 if a file was choosen save the current folder and # the next time the file chooser is used in this session the same folder opens self.current_folder = self.fcd.get_current_folder() self.fcd.destroy() anything() gtk.main()
The File