On Sun, Jul 11, 2004 at 04:56:08PM -0500, Alex Roitman wrote: > > Attached is the first draft of the FileChooser section for the PyGtk > tutorial. This is a patch against MiscellaneousWidgets.xml from CVS > as of Sunday afternoon. I am also attaching filechooser.py which > should go into examples. This file is almost verbatim copy of > gnome-python/pygtk/examples/gtk/filechooser.py, I hope it's not a problem.
Of course, I forgot to actually attach the files. Here you go. Sorry for the trouble, Alex -- Alexander Roitman http://ebner.neuroscience.umn.edu/people/alex.html Dept. of Neuroscience, Lions Research Building 2001 6th Street SE, Minneapolis, MN 55455 Tel (612) 625-7566 FAX (612) 626-9201
? examples/filechooser.py
Index: tut/MiscellaneousWidgets.xml
===================================================================
RCS file: /cvs/gnome/pygtk-docs/2.0/tut/MiscellaneousWidgets.xml,v
retrieving revision 1.4
diff -u -r1.4 MiscellaneousWidgets.xml
--- tut/MiscellaneousWidgets.xml 8 Jul 2004 08:03:52 -0000 1.4
+++ tut/MiscellaneousWidgets.xml 11 Jul 2004 21:47:34 -0000
@@ -3250,6 +3250,168 @@
</sect1>
<!-- ===================================================================== -->
+ <sect1 id="sec-FileChoosers">
+ <title>File Selections using FileChooser-based Widgets</title>
+
+ <para>The new way to select files in PyGTK 2.4 is to use the variants
+ of the <classname>FileChooser</classname> widget. The two objects that
+ implement this new interface in PyGTK 2.4 are
+ <classname>FileChooserWidget</classname> and
+ <classname>FileChooserDialog</classname>.
+ The latter is the complete dialog with the window and easily defined
+ buttons. The former is a widget useful for embedding within another
+ widget.
+ </para>
+
+ <para>Both <classname>FileChooserWidget</classname> and
+ <classname>FileChooserDialog</classname> possess the means for
+ navigating the filesystem tree and selecting files. The view of
+ the widgets depends on the action used to open a widget. </para>
+
+ <para>To create a new file chooser dialog to select an existing file
+ (as in typical File->Open option of a typical application), use:</para>
+
+ <programlisting>
+ chooser = gtk.FileChooserDialog(<parameter
role="keyword">title</parameter>=None,<parameter
role="keyword">action</parameter>=gtk.FILE_CHOOSER_ACTION_OPEN,<parameter
role="keyword">buttons</parameter>=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))
+</programlisting>
+
+ <para>To create a new file chooser dialog to select a new file name
+ (as in typical File->Save as option of a typical application), use:</para>
+
+ <programlisting>
+ chooser = gtk.FileChooserDialog(<parameter
role="keyword">title</parameter>=None,<parameter
role="keyword">action</parameter>=gtk.FILE_CHOOSER_ACTION_SAVE,<parameter
role="keyword">buttons</parameter>=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK))
+</programlisting>
+
+ <para>In the above examples, the two buttons (stock Cancel and Open items)
+ are created and connected to their respective responses (stock Cancel
+ and OK responses).
+ </para>
+
+ <para>To set the folder displayed in the file chooser, use
+ use this method:</para>
+
+ <programlisting>
+ chooser.set_current_folder(<parameter role="keyword">pathname</parameter>)
+</programlisting>
+
+ <para>To set the suggested file name as if it was typed by a user
+ (typical Save as situation), use this method:</para>
+
+ <programlisting>
+ chooser.set_current_name(<parameter role="keyword">name</parameter>)
+</programlisting>
+
+ <para>Note that the above method does not require the filename to exist.
+ If you want to preselect a particular existing file (as in File->Open
+ situation), you should use this method:</para>
+
+ <programlisting>
+ chooser.set_filename(<parameter role="keyword">filename</parameter>)
+</programlisting>
+
+ <para>To obtain the filename that the user has entered or clicked
+on, use this method:</para>
+
+ <programlisting>
+ filename = chooser.get_filename()
+</programlisting>
+
+ <para>It is possible to allow multiple file selections (only for the
+ gtk.FILE_CHOOSER_ACTION_OPEN action) by using this method:</para>
+
+ <programlisting>
+ chooser.set_select_multiple(gtk.TRUE)
+</programlisting>
+
+ <para>In that case, you will need to use the following method to
+ retrieve a list of selected filenames:</para>
+
+ <programlisting>
+ filename = chooser.get_filenames()
+</programlisting>
+
+ <para>An important feature of all file choosers is the ability
+ to add file selection filters. The filter may be added by
+ this method:</para>
+
+ <programlisting>
+ chooser.add_filter(filter)
+</programlisting>
+
+ <para>In the example above, the filter must be an instance
+ of the <classname>FileFilter</classname>class. </para>
+
+ <para>The left panel of the file chooser lists some shortcut folders
+ such as Home, Filesystem, CDROM, etc. You may add a folder to the list
+ of these shortcuts and remove it from the list by using these
+ methods:</para>
+
+ <programlisting>
+ chooser.add_shortcut_folder(pathname)
+ chooser.remove_shortcut_folder(pathname)
+</programlisting>
+
+ <para>The <ulink
+url="examples/filechooser.py"><command>filechooser.py</command></ulink>
+example program illustrates the use of the filechooser widget.
+<xref linkend="filechooserfig"/> shows the resulting display:</para>
+
+ <figure id="filechooserfig">
+ <title>File Selection Example</title>
+ <mediaobject>
+ <imageobject>
+ <imagedata fileref="figures/filechooser.png" format="png" align="center"/>
+ </imageobject>
+ </mediaobject>
+ </figure>
+
+ <para>The source code for filechooser.py is:</para>
+
+ <programlisting>
+ 1 #!/usr/bin/env python
+ 2
+ 3 # example filechooser.py
+ 4
+ 5 import pygtk
+ 6 pygtk.require('2.0')
+ 7
+ 8 import gtk
+ 9
+ 10 # Check for new pygtk: this is new class in PyGtk 2.4
+ 11 if gtk.pygtk_version < (2,3,90):
+ 12 print "PyGtk 2.3.90 or later required for this example"
+ 13 raise SystemExit
+ 14
+ 15 dialog = gtk.FileChooserDialog("Open..",
+ 16 None,
+ 17
gtk.FILE_CHOOSER_ACTION_OPEN,
+ 18 (gtk.STOCK_CANCEL,
gtk.RESPONSE_CANCEL,
+ 19 gtk.STOCK_OPEN,
gtk.RESPONSE_OK))
+ 20 dialog.set_default_response(gtk.RESPONSE_OK)
+ 21
+ 22 filter = gtk.FileFilter()
+ 23 filter.set_name("All files")
+ 24 filter.add_pattern("*")
+ 25 dialog.add_filter(filter)
+ 26
+ 27 filter = gtk.FileFilter()
+ 28 filter.set_name("Images")
+ 29 filter.add_mime_type("image/png")
+ 30 filter.add_mime_type("image/jpeg")
+ 31 filter.add_mime_type("image/gif")
+ 32 dialog.add_filter(filter)
+ 33
+ 34 response = dialog.run()
+ 35 dialog.destroy()
+ 36 if response == gtk.RESPONSE_OK:
+ 37 print dialog.get_filename(), 'selected'
+ 38 elif response == gtk.RESPONSE:
+ 39 print 'Closed, no files selected'
+ </programlisting>
+
+ </sect1>
+
+<!-- ===================================================================== -->
<sect1 id="sec-FontSelectionDialog">
<title>Font Selection Dialog</title>
#!/usr/bin/env python
# example filechooser.py
import pygtk
pygtk.require('2.0')
import gtk
# Check for new pygtk: this is new class in PyGtk 2.4
if gtk.pygtk_version < (2,3,90):
print "PyGtk 2.3.90 or later required for this example"
raise SystemExit
dialog = gtk.FileChooserDialog("Open..",
None,
gtk.FILE_CHOOSER_ACTION_OPEN,
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_OPEN, gtk.RESPONSE_OK))
dialog.set_default_response(gtk.RESPONSE_OK)
filter = gtk.FileFilter()
filter.set_name("All files")
filter.add_pattern("*")
dialog.add_filter(filter)
filter = gtk.FileFilter()
filter.set_name("Images")
filter.add_mime_type("image/png")
filter.add_mime_type("image/jpeg")
filter.add_mime_type("image/gif")
dialog.add_filter(filter)
response = dialog.run()
dialog.destroy()
if response == gtk.RESPONSE_OK:
print dialog.get_filename(), 'selected'
elif response == gtk.RESPONSE:
print 'Closed, no files selected'
signature.asc
Description: Digital signature
_______________________________________________ pygtk mailing list [EMAIL PROTECTED] http://www.daa.com.au/mailman/listinfo/pygtk Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
