9.14. File Selections

The file selection widget is a quick and simple way to display a File dialog box. It comes complete with Ok, Cancel, and Help buttons, a great way to cut down on programming time.

To create a new file selection box use:

  filesel = gtk.FileSelection(title=None)

To set the filename, for example to bring up a specific directory, or give a default filename, use this method:

  filesel.set_filename(filename)

To grab the filename text that the user has entered or clicked on, use this method:

  filename = filesel.get_filename()

There are also references to the widgets contained within the file selection widget. These are the filesel attributes:

  filesel.dir_list
  filesel.file_list
  filesel.selection_entry
  filesel.selection_text
  filesel.main_vbox
  filesel.ok_button
  filesel.cancel_button
  filesel.help_button
  filesel.history_pulldown
  filesel.history_menu
  filesel.fileop_dialog
  filesel.fileop_entry
  filesel.fileop_file
  filesel.fileop_c_dir
  filesel.fileop_del_file
  filesel.fileop_ren_file
  filesel.button_area
  filesel.action_area

Most likely you will want to use the ok_button, cancel_button, and help_button attributes to connect their widget signals to callbacks.

The filesel.py example program illustrates the use of the FileSelection widget. As you will see, there is nothing much to creating a file selection widget. While in this example the Help button appears on the screen, it does nothing as there is not a signal attached to it. Figure 9.14, “File Selection Example” shows the resulting display:

Figure 9.14. File Selection Example

File Selection Example

The source code for filesel.py is:

    1	#!/usr/bin/env python
    2	
    3	# example filesel.py
    4	
    5	import pygtk
    6	pygtk.require('2.0')
    7	import gtk
    8	
    9	class FileSelectionExample:
   10	    # Get the selected filename and print it to the console
   11	    def file_ok_sel(self, w):
   12	        print "%s" % self.filew.get_filename()
   13	
   14	    def destroy(self, widget):
   15	        gtk.main_quit()
   16	
   17	    def __init__(self):
   18	        # Create a new file selection widget
   19	        self.filew = gtk.FileSelection("File selection")
   20	
   21	        self.filew.connect("destroy", self.destroy)
   22	        # Connect the ok_button to file_ok_sel method
   23	        self.filew.ok_button.connect("clicked", self.file_ok_sel)
   24	    
   25	        # Connect the cancel_button to destroy the widget
   26	        self.filew.cancel_button.connect("clicked",
   27	                                         lambda w: self.filew.destroy())
   28	    
   29	        # Lets set the filename, as if this were a save dialog,
   30	        # and we are giving a default filename
   31	        self.filew.set_filename("penguin.png")
   32	    
   33	        self.filew.show()
   34	
   35	def main():
   36	    gtk.main()
   37	    return 0
   38	
   39	if __name__ == "__main__":
   40	    FileSelectionExample()
   41	    main()