Gtk File Chooser Dialog

Setting up the Python

First we set up the minumum Python to load our glade and shut down properly. We must define functions for every signal handler we created in the glade file. For now we just have functions that do nothing with the pass except for the function that is connected to window1 destroy handler on_window1_destroy.

gtk10.py
#!/usr/bin/env python

import gtk

class main:
  def __init__(self):
    self.builder = gtk.Builder()
    self.builder.add_from_file('gtk10.glade')
    self.builder.connect_signals(self)
    self.window = self.builder.get_object("window1")
    self.window.show()

  def on_file_new_activate(self, menuitem, data=None):
    pass

  def on_file_open_activate(self, menuitem, data=None):
    pass

  def on_file_save_activate(self, menuitem, data=None):
    pass

  def on_file_save_as_activate(self, menuitem, data=None):
    pass

  def on_file_quit_activate(self, menuitem, data=None):
    pass

  def on_window1_destroy(self, object, data=None):
    print "quit with cancel"
    gtk.main_quit()

if __name__ == "__main__":
  main = main()
  gtk.main()

Save the Python file as gtk10.py and set the permission to execute.

When you run gtk10.py your project should look like this:

images/gtk-10-04.png

At this point the only thing that works is if you click on the X the window will politely go away.

Creating a File Chooser Dialog

IMHO creating a file chooser dialog in Python is easier and cleaner than using the Glade file chooser dialog. By creating it in Python you also have more choices.

The syntax for the Gtk file chooser dialog is:

gtk.FileChooserDialog(title, parent, action, buttons, backend)
  • title = The window title

  • parent = Usually set to None

  • action = One of the Gtk file chooser actions:

    • gtk.FILE_CHOOSER_ACTION_OPEN pick an existing file.

    • gtk.FILE_CHOOSER_ACTION_SAVE pick an existing file, or type in a new filename.

    • gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER pick an existing folder.

    • gtk.FILE_CHOOSER_ACTION_CREATE_FOLDER name an existing or new folder

  • buttons = A pair containing a Gtk stock item
    [Gtk Stock Items]
    and a response.
    [Gtk Response Type Constants]
    The following partial list is appropriate for the file chooser dialog.

    • gtk.STOCK_OPEN

    • gtk.STOCK_SAVE

    • gtk.STOCK_SAVE_AS

    • gtk.STOCK_CANCEL

  • backend = dunno what that is and we won’t use it anyway.

In the following example we create a file open dialog. Note the syntax for the buttons is '(gtk stock item, gtk response)

Example File Chooser Dialog
def on_file_open_activate(self, menuitem, data=None):
  self.fcd = gtk.FileChooserDialog("Open...",
             None,
             gtk.FILE_CHOOSER_ACTION_OPEN,
             (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK))
  self.response = self.fcd.run()
  if self.response == gtk.RESPONSE_OK:
    print "Selected filepath: %s" % self.fcd.get_filename()
    self.fcd.destroy()

Add this snippet to the Python file and run, then select file open from the menu. Make sure you remove the pass from the function.

Our file chooser dialog should look like this:

images/gtk-10-05.png

The file chooser dialog has many options
[FileChooser]
to choose from. We will explore one to show you how to add a path to the shortcut menu of the file chooser dialog.

First add import os to the top of the Python code under import gtk.

add_shortcut_folder
# first we check to see if the folder exists by using os.path.isdir
# which returns true or false
if os.path.isdir(os.getenv("HOME")+'/linuxcnc/configs'):
  # if the directory is there we add it to the shortcut folder
  self.fcd.add_shortcut_folder(os.getenv("HOME")+'/linuxcnc/configs')
Update 10-2015 stop annoying file chooser using recent files

These three items noted by numbers make the file chooser use a sane choice and display first the users home directory then if a file is chosen the next time displays the chosen directory.

gtk10.py
#!/usr/bin/env python
version = '0.0.7'

import gtk
import os

class main:
  def __init__(self):
    self.builder = gtk.Builder()
    self.builder.add_from_file('gtk10.glade')
    self.builder.connect_signals(self)
    self.window = self.builder.get_object("window1")
    # 1 get the users home directory
    self.current_folder = os.path.expanduser('~')
    self.window.show()

  def on_file_new_activate(self, menuitem, data=None):
    pass

  def on_file_open_activate(self, menuitem, data=None):
    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)
    if os.path.isdir(os.getenv("HOME")+'/linuxcnc/configs'):
      self.fcd.add_shortcut_folder(os.getenv("HOME")+'/linuxcnc/configs')
    self.response = self.fcd.run()
    if self.response == gtk.RESPONSE_OK:
      print "Selected filepath: %s" % self.fcd.get_filename()
      print self.fcd.get_uri()
      # 3 if a file was choosen save the current folder
      self.current_folder = self.fcd.get_current_folder()
      self.fcd.destroy()

  def on_file_save_activate(self, menuitem, data=None):
    pass

  def on_file_save_as_activate(self, menuitem, data=None):
    pass

  def on_file_quit_activate(self, menuitem, data=None):
    pass

  def on_window1_destroy(self, object, data=None):
    print "quit with cancel"
    gtk.main_quit()

if __name__ == "__main__":
  main = main()
  gtk.main()

Now when we run the code we see the shortcut if it is there. Clicking on the configs short cut switched the file chooser dialog to that directory.

images/gtk-10-06.png

The files used in this tutorial: