# SinglePictureGTK.java (c) Kari Laitinen # http://www.naturalprogramming.com/ # 2006-11-17 File created. # 2006-11-23 Last modification. # This program demonstrates how a Python/GTK program can show pictures. # The picture file must be in the same folder where this program is located. # In this program, a picture is handled as a Pixbuf object. Class Pixbuf # provides a nice set of methods for manipulating pixel information. # More notes at the end of this file. import gtk class SinglePictureDemo : def __init__( self ) : self.application_window = gtk.Window( gtk.WINDOW_TOPLEVEL ) self.application_window.set_title( "SINGLE PICTURE IN SEVERAL SIZES" ) self.application_window.connect("destroy", lambda w: gtk.main_quit() ) self.application_window.set_size_request( 800, 480 ) self.drawing_area = gtk.DrawingArea() self.application_window.add( self.drawing_area ) self.drawing_area.connect( "expose-event", self.drawing_area_exposed ) self.picture_as_pixel_buffer = gtk.gdk.pixbuf_new_from_file( "governor_arnold_schwarzenegger.jpg") self.application_window.show_all() def drawing_area_exposed( self, area, event ) : graphics_context = self.drawing_area.get_style().fg_gc[gtk.STATE_NORMAL] this_drawable = self.drawing_area.window window_width, window_height = self.application_window.get_size() picture_width = self.picture_as_pixel_buffer.get_width() picture_height = self.picture_as_pixel_buffer.get_height() picture_position_x = 15 picture_position_y = 15 this_drawable.draw_pixbuf( graphics_context, self.picture_as_pixel_buffer, 0, 0, picture_position_x, picture_position_y ) picture_position_x = picture_position_x + picture_width + 10 # Next we'll show a smaller version of the picture. small_picture_version = \ self.picture_as_pixel_buffer.scale_simple( picture_width / 2, picture_height / 2, gtk.gdk.INTERP_BILINEAR ) this_drawable.draw_pixbuf( graphics_context, small_picture_version, 0, 0, picture_position_x, picture_position_y ) picture_position_x = picture_position_x + picture_width / 2 + 10 # Finally, we'll show the picture so that its width is enlarged. widened_picture_version = \ self.picture_as_pixel_buffer.scale_simple( int( picture_width * 1.5 ), picture_height, gtk.gdk.INTERP_BILINEAR ) this_drawable.draw_pixbuf( graphics_context, widened_picture_version, 0, 0, picture_position_x, picture_position_y ) def run( self ) : gtk.main() if __name__ == "__main__": this_application = SinglePictureDemo() this_application.run() # NOTES: # - Pictures can also be handled as Image objects. # If you want to show a single picture in its natural size, # you can do it with the following statements in an __init__() # method. # self.picture_to_show = gtk.Image() # self.picture_to_show.set_from_file( "governor_arnold_schwarzenegger.jpg") # self.application_window.add( self.picture_to_show ) # self.application_window.show_all() # - An Image object can be put inside an EventBox object if it is # necessary to do something when the image is clicked with the # mouse.