diff options
author | Gustavo J. A. M. Carneiro <gcarneiro@src.gnome.org> | 2004-04-10 10:51:47 +0000 |
---|---|---|
committer | Gustavo J. A. M. Carneiro <gcarneiro@src.gnome.org> | 2004-04-10 10:51:47 +0000 |
commit | fd5fd15b235ba7a36ca1465687262d71c96d140e (patch) | |
tree | e21ad6927fe3e190c77c22a5dfffd9ec72f4de5f | |
parent | b5fae0bc54381054480b5e1ff053cfbecf802873 (diff) | |
download | pygtk-fd5fd15b235ba7a36ca1465687262d71c96d140e.tar.gz |
Add 'Images' demo from Bug 100873: All demos from gtk-demo should be ported
-rw-r--r-- | ChangeLog | 8 | ||||
-rw-r--r-- | examples/pygtk-demo/alphatest.png | bin | 0 -> 26529 bytes | |||
-rw-r--r-- | examples/pygtk-demo/demos/images.py | 163 | ||||
-rw-r--r-- | examples/pygtk-demo/floppybuddy.gif | bin | 0 -> 5216 bytes |
4 files changed, 171 insertions, 0 deletions
@@ -1,3 +1,11 @@ +2004-04-10 Gustavo J. A. M. Carneiro <gustavo@users.sourceforge.net> + + * examples/pygtk-demo/demos/images.py: Add 'Images' demo ported to + python by Joey Tsai (see bug #100873). + + * Makefile.am (EXTRA_DIST): Add floppybuddy.gif and alphatest.png, + copied from gtk+, for pygtk-demo. + 2004-04-09 John Finlay <finlay@moeraki.com> * gtk/gtk.defs (gtk_action_group_add_action_with_accel) The accelerator diff --git a/examples/pygtk-demo/alphatest.png b/examples/pygtk-demo/alphatest.png Binary files differnew file mode 100644 index 00000000..eb5885f8 --- /dev/null +++ b/examples/pygtk-demo/alphatest.png diff --git a/examples/pygtk-demo/demos/images.py b/examples/pygtk-demo/demos/images.py new file mode 100644 index 00000000..b6ab1b78 --- /dev/null +++ b/examples/pygtk-demo/demos/images.py @@ -0,0 +1,163 @@ +#! /usr/bin/python2.3 +# +# Copyright (C) 2004 Joey Tsai <joeytsai@joeytsai.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. +"""Images + +GtkImage is used to display an image; the image can be in a number of formats. +Typically, you load an image into a GdkPixbuf, then display the pixbuf. + +This demo code shows some of the more obscure cases, in the simple case a call +to gtk.Image's set_from_file() is all you need. + +""" + +import sys + +import pygtk; pygtk.require("2.0") +import gobject +import gtk + +description = "Images" + + +def error_dialog( message, parent=None ): + dialog = gtk.MessageDialog( parent, + gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, + gtk.MESSAGE_WARNING, gtk.BUTTONS_OK, message ) + dialog.run() + dialog.destroy() + +def on_timeout( loader, fileobject ): + run_again = True + + try: + bytes = fileobject.read( 256 ) + loader.write( bytes ) + except (IOError, gobject.GError), e: + error_dialog( str(e) ) + run_again = False + + if bytes == "": + run_again = False + + if not run_again: + try: + fileobject.close() + loader.close() + except gobject.GError, e: + # bug 136989, loader.close() will throw an exception + pass + + return run_again + +def on_area_prepared( loader, image ): + pixbuf = loader.get_pixbuf() + image.set_from_pixbuf( pixbuf ) + +def on_area_updated( loader, x, y, w, h, image ): + image.queue_draw() + +def get_progressive_image( filename ): + try: + fileobject = file( filename, "r" ) + except IOError, e: + error_dialog( str(e) ) + return None + + image = gtk.Image() + + loader = gtk.gdk.PixbufLoader() + loader.connect( "area_prepared", on_area_prepared, image ) + loader.connect( "area_updated", on_area_updated, image ) + + gobject.timeout_add( 150, on_timeout, loader, fileobject ) + + return image + +def get_image( filename ): + try: + anim = gtk.gdk.PixbufAnimation( filename ) + except gobject.GError, e: + error_dialog( str(e) ) + return None + + image = gtk.Image() + + if anim.is_static_image(): + image.set_from_pixbuf( anim.get_static_image() ) + else: + image.set_from_animation( anim ) + + return image + +def align_image( image, label_text ): + label = gtk.Label() + label.set_markup( "<u>%s</u>" % label_text ) + + frame = gtk.Frame() + frame.set_shadow_type( gtk.SHADOW_IN ) + + if image: + frame.add( image ) + else: + frame.add( gtk.Label("(No image)") ) + + align = gtk.Alignment( 0.5, 0.5, 0, 0 ) + align.add( frame ) + + vbox = gtk.VBox( spacing=8 ) + vbox.set_border_width( 4 ) + vbox.pack_start( label ) + vbox.pack_start( align ) + + return vbox + +def on_button_toggled( button, vbox ): + for widget in vbox.get_children(): + if widget != button: + widget.set_sensitive( not button.get_active() ) + + +def main(): + vbox = gtk.VBox() + + button = gtk.ToggleButton( "_Insensitive" ) + button.connect( "toggled", on_button_toggled, vbox ) + + i1 = get_image( "gtk-logo-rgb.gif" ) + i2 = get_image( "floppybuddy.gif" ) + i3 = get_progressive_image( "alphatest.png" ) + + vbox.pack_start( align_image(i1, "Image loaded from a file") ) + vbox.pack_start( align_image(i2, "Animation loaded from a file") ) + vbox.pack_start( align_image(i3, "Progressive image loading") ) + vbox.pack_start( button ) + + win = gtk.Window() + win.set_title( "Images" ) + win.set_border_width( 8 ) + win.connect( "destroy", lambda w: gtk.main_quit() ) + win.add( vbox ) + win.show_all() + + gtk.main() + + return 0 + +if __name__ == "__main__": + sys.exit( main() ) diff --git a/examples/pygtk-demo/floppybuddy.gif b/examples/pygtk-demo/floppybuddy.gif Binary files differnew file mode 100644 index 00000000..ac986c8e --- /dev/null +++ b/examples/pygtk-demo/floppybuddy.gif |