Python-Ref > GUI programming with PyGTK > Dialogs > File selection dialog
 
 

<-^^

File selection dialog

How to use a dialog for file selection.
PyGTK contains a standard FileChooserDialog.
The following simple example shows the basic workings of a dialog.
Expand/Shrink
  1   # ensure that PyGTK 2.0 is loaded - not an older version
  2   import pygtk
  3   pygtk.require('2.0')
  4   # import the GTK module
  5   import gtk
  6   
  7   class MyGUI:
  8   
  9     def __init__( self, title):
 10       self.window = gtk.Window()
 11       self.title = title
 12       self.window.set_title( title)
 13       self.window.set_size_request( -1, -1)
 14       self.window.connect( "destroy", self.destroy)
 15       self.create_interior()
 16       self.window.show_all()
 17   
 18     def create_interior( self):
 19       self.mainbox = gtk.VBox()
 20       self.window.add( self.mainbox)
 21       # label
 22       self.label_template = "File: <b>%s</b>\nDir: <b>%s</b>\nSize: <b>%s</b>"
 23       self.label = gtk.Label( self.label_template % ("","",""))
 24       self.label.set_use_markup( True)
 25       self.mainbox.pack_start( self.label, padding=10)
 26       self.label.show()
 27       # button that triggers the FileChooserDialog
 28       b = gtk.Button( "Select file..")
 29       self.mainbox.pack_start( b, expand=False)
 30       b.show()
 31       b.connect( "clicked", self.open_file)
 32       # show the box
 33       self.mainbox.show()
 34   
 35     def main( self):
 36       gtk.main()
 37   
 38     def destroy( self, w):
 39       gtk.main_quit()
 40   
 41     def open_file( self, w, data=None):
 42       d = gtk.FileChooserDialog( title="Select a file",
 43                                  parent=self.window,
 44                                  action=gtk.FILE_CHOOSER_ACTION_OPEN,
 45                                  buttons=("OK",True,"Cancel",False)
 46                                  )
 47       ok = d.run()
 48       if ok:
 49         import os
 50         fullname = d.get_filename()
 51         dirname, fname = os.path.split( fullname)
 52         size = "%d bytes" % os.path.getsize( fullname)
 53         text = self.label_template % (fname, dirname, size)
 54       else:
 55         text = self.label_template % ("","","")
 56       self.label.set_label( text)
 57       # make the dialog disappear, don't do it before you get all data from it
 58       d.destroy() 
 59   
 60   
 61   if __name__ == "__main__":
 62     m = MyGUI( "Open File")
 63     m.main()
Screenshot:
Program screenshot gtk-fileopen1-1.pngProgram screenshot gtk-fileopen1-1a.pngProgram screenshot gtk-fileopen1-1b.png
Doba běhu: 3179.3 ms
When adding buttons to the dialog, we can use stock buttons and stock responses. This will create nicer and themable buttons and as a side-effect cause the OK button to be activated when "Return" is pressed inside the dialog window.
Expand/Shrink
Rozdíl proti: gtk-fileopen1-1.py
@@ -42,10 +42,11 @@
     d = gtk.FileChooserDialog( title="Select a file",
                                parent=self.window,
                                action=gtk.FILE_CHOOSER_ACTION_OPEN,
-                               buttons=("OK",True,"Cancel",False)
+                               buttons=(gtk.STOCK_OK,gtk.RESPONSE_ACCEPT,
+                                        gtk.STOCK_CANCEL,gtk.RESPONSE_REJECT)
                                )
     ok = d.run()
-    if ok:
+    if ok == gtk.RESPONSE_ACCEPT:
       import os
       fullname = d.get_filename()
       dirname, fname = os.path.split( fullname)
Screenshot:
Program screenshot gtk-fileopen1-2.pngProgram screenshot gtk-fileopen1-2a.pngProgram screenshot gtk-fileopen1-2b.png
Doba běhu: 2866.2 ms
When opening files, it is very common to provide a filter that will limit the types of files that are visible in the dialog. To do this, we can use the add_filter() and set_filter() methods of the dialog.
Expand/Shrink
Rozdíl proti: gtk-fileopen1-2.py
@@ -45,6 +45,17 @@
                                buttons=(gtk.STOCK_OK,gtk.RESPONSE_ACCEPT,
                                         gtk.STOCK_CANCEL,gtk.RESPONSE_REJECT)
                                )
+    # create filters
+    f1 = gtk.FileFilter()
+    f1.set_name( "All files")
+    f1.add_pattern( "*")
+    d.add_filter( f1)
+    f2 = gtk.FileFilter()
+    f2.set_name( "Python scripts")
+    f2.add_pattern( "*.py")
+    f2.add_pattern( "*.psp")
+    d.add_filter( f2)
+    # run the dialog
     ok = d.run()
     if ok == gtk.RESPONSE_ACCEPT:
       import os
Screenshot:
Program screenshot gtk-fileopen1-3.pngProgram screenshot gtk-fileopen1-3a.pngProgram screenshot gtk-fileopen1-3b.png
Doba běhu: 643.0 ms