home *** CD-ROM | disk | FTP | other *** search
/ tusportal.tus.k12.pa.us / tusportal.tus.k12.pa.us.tar / tusportal.tus.k12.pa.us / Wyse / latest-image.raw / 0.img / usr / lib / pygtk / 2.0 / demos / images.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2010-05-11  |  8KB  |  208 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """Images
  5.  
  6. GtkImage is used to display an image; the image can be in a number of formats.
  7. Typically, you load an image into a GdkPixbuf, then display the pixbuf.
  8. This demo code shows some of the more obscure cases, in the simple case a call
  9. to gtk_image_new_from_file() is all you need.
  10. If you want to put image data in your program as a C variable, use the
  11. make-inline-pixbuf program that comes with GTK+. This way you won't need to
  12. depend on loading external files, your application binary can be self-contained."""
  13. import os
  14. import gobject
  15. import gtk
  16. IMAGEDIR = os.path.join(os.path.dirname(__file__), 'images')
  17. ALPHA_IMAGE = os.path.join(IMAGEDIR, 'alphatest.png')
  18. GTKLOGO_IMAGE = os.path.join(IMAGEDIR, 'gtk-logo-rgb.gif')
  19. BUDDY_IMAGE = os.path.join(IMAGEDIR, 'floppybuddy.gif')
  20.  
  21. def progressive_prepared_callback(loader, image):
  22.     pixbuf = loader.get_pixbuf()
  23.     pixbuf.fill(0)
  24.     image.set_from_pixbuf(pixbuf)
  25.  
  26.  
  27. def progressive_updated_callback(loader, x, y, width, height, image):
  28.     """ We know the pixbuf inside the GtkImage has changed, but the image
  29.         itself doesn't know this; so queue a redraw.  If we wanted to be
  30.         really efficient, we could use a drawing area or something
  31.         instead of a GtkImage, so we could control the exact position of
  32.         the pixbuf on the display, then we could queue a draw for only
  33.         the updated area of the image.
  34.     """
  35.     image.queue_draw()
  36.  
  37.  
  38. class ImagesDemo(gtk.Window):
  39.     pixbuf_loader = None
  40.     load_timeout = None
  41.     image_stream = None
  42.     
  43.     def __init__(self, parent = None):
  44.         gtk.Window.__init__(self)
  45.         
  46.         try:
  47.             self.set_screen(parent.get_screen())
  48.         except AttributeError:
  49.             self.connect('destroy', (lambda : gtk.main_quit()))
  50.  
  51.         self.connect('destroy', self.cleanup_callback)
  52.         self.set_title(self.__class__.__name__)
  53.         self.set_border_width(8)
  54.         vbox = gtk.VBox(False, 8)
  55.         vbox.set_border_width(8)
  56.         self.add(vbox)
  57.         label = gtk.Label()
  58.         label.set_markup('<u>Image loaded from a file</u>')
  59.         vbox.pack_start(label, False, False, 0)
  60.         frame = gtk.Frame()
  61.         frame.set_shadow_type(gtk.SHADOW_IN)
  62.         align = gtk.Alignment(0.5, 0.5, 0, 0)
  63.         align.add(frame)
  64.         vbox.pack_start(align, False, False, 0)
  65.         image = gtk.Image()
  66.         
  67.         try:
  68.             pixbuf = gtk.gdk.pixbuf_new_from_file(GTKLOGO_IMAGE)
  69.             image.set_from_pixbuf(pixbuf)
  70.         except gobject.GError:
  71.             error = None
  72.             dialog = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, "Unable to open image file 'gtk-logo-rgb.gif': \n%s" % error)
  73.             dialog.connect('response', (lambda dlg, resp: dlg.destroy()))
  74.             dialog.show()
  75.  
  76.         frame.add(image)
  77.         label = gtk.Label()
  78.         label.set_markup('<u>Animation loaded from a file</u>')
  79.         vbox.pack_start(label, False, False, 0)
  80.         frame = gtk.Frame()
  81.         frame.set_shadow_type(gtk.SHADOW_IN)
  82.         align = gtk.Alignment(0.5, 0.5, 0, 0)
  83.         align.add(frame)
  84.         vbox.pack_start(align, False, False, 0)
  85.         image = gtk.Image()
  86.         image.set_from_file(BUDDY_IMAGE)
  87.         frame.add(image)
  88.         label = gtk.Label()
  89.         label.set_markup('<u>Progressive image loading</u>')
  90.         vbox.pack_start(label, False, False, 0)
  91.         frame = gtk.Frame()
  92.         frame.set_shadow_type(gtk.SHADOW_IN)
  93.         align = gtk.Alignment(0.5, 0.5, 0, 0)
  94.         align.add(frame)
  95.         vbox.pack_start(align, False, False, 0)
  96.         image = gtk.Image()
  97.         image.set_from_pixbuf(None)
  98.         frame.add(image)
  99.         self.start_progressive_loading(image)
  100.         button = gtk.ToggleButton('_Insensitive')
  101.         vbox.pack_start(button, False, False, 0)
  102.         button.connect('toggled', self.on_sensitivity_toggled, vbox)
  103.         self.show_all()
  104.  
  105.     
  106.     def cleanup_callback(self, win):
  107.         if self.load_timeout != 0:
  108.             gtk.timeout_remove(self.load_timeout)
  109.             self.load_timeout = 0
  110.         
  111.         if self.pixbuf_loader is not None:
  112.             self.pixbuf_loader.close()
  113.             self.pixbuf_loader = None
  114.         
  115.         if self.image_stream is not None:
  116.             self.image_stream.close()
  117.             self.image_stream = None
  118.         
  119.  
  120.     
  121.     def on_sensitivity_toggled(self, togglebutton, container):
  122.         children = container.get_children()
  123.         for child in children:
  124.             if type(child) != type(togglebutton):
  125.                 child.set_sensitive(not togglebutton.get_active())
  126.                 continue
  127.         
  128.  
  129.     
  130.     def start_progressive_loading(self, image):
  131.         ''' This is obviously totally contrived(we slow down loading
  132.             on purpose to show how incremental loading works).
  133.             The real purpose of incremental loading is the case where
  134.             you are reading data from a slow source such as the network.
  135.             The timeout simply simulates a slow data source by inserting
  136.             pauses in the reading process.
  137.         '''
  138.         self.load_timeout = gtk.timeout_add(150, self.progressive_timeout, image)
  139.  
  140.     
  141.     def progressive_timeout(self, image):
  142.         if self.image_stream is not None:
  143.             
  144.             try:
  145.                 buf = self.image_stream.read(256)
  146.                 bytes_read = len(buf)
  147.             except IOError:
  148.                 error = None
  149.                 dialog = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, "Failure reading image file 'alphatest.png': %s" % error)
  150.                 dialog.connect('response', (lambda d, r: d.destroy()))
  151.                 self.image_stream.close()
  152.                 self.image_stream = None
  153.                 dialog.show()
  154.                 self.load_timeout = 0
  155.                 return False
  156.  
  157.             if not self.pixbuf_loader.write(buf, bytes_read):
  158.                 dialog = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, 'Failed to load image')
  159.                 dialog.connect('response', (lambda d, r: d.destroy()))
  160.                 self.image_stream.close()
  161.                 self.image_stream = None
  162.                 dialog.show()
  163.                 self.load_timeout = 0
  164.                 return False
  165.             if bytes_read == 0:
  166.                 self.image_stream.close()
  167.                 self.image_stream = None
  168.                 if not self.pixbuf_loader.close():
  169.                     dialog = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, 'Failed to load image')
  170.                     dialog.connect('response', (lambda d, r: d.destroy()))
  171.                     dialog.show()
  172.                     self.pixbuf_loader = None
  173.                     self.load_timeout = 0
  174.                     return False
  175.                 self.pixbuf_loader = None
  176.             
  177.         else:
  178.             
  179.             try:
  180.                 self.image_stream = open(ALPHA_IMAGE, 'rb')
  181.             except IOError:
  182.                 error = None
  183.                 error_message = "Unable to open image file 'alphatest.png' : %s"
  184.                 dialog = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, error_message % error)
  185.                 dialog.connect('response', (lambda d, r: d.destroy()))
  186.                 dialog.show()
  187.                 self.load_timeout = 0
  188.                 return False
  189.  
  190.             if self.pixbuf_loader is not None:
  191.                 self.pixbuf_loader.close()
  192.                 self.pixbuf_loader = None
  193.             
  194.             self.pixbuf_loader = gtk.gdk.PixbufLoader()
  195.             self.pixbuf_loader.connect('area_prepared', progressive_prepared_callback, image)
  196.             self.pixbuf_loader.connect('area_updated', progressive_updated_callback, image)
  197.         return True
  198.  
  199.  
  200.  
  201. def main():
  202.     ImagesDemo()
  203.     gtk.main()
  204.  
  205. if __name__ == '__main__':
  206.     main()
  207.  
  208.