Python-Ref > GUI programming with PyGTK > Widgets > Image
 
 

<-^^->

Image

How to display an image.
Placing an image into your gui is as simple as using a Label. The following code shows how to create an image either from a file or from a stock item. It also shows how to add an image onto a button.
Expand/Shrink
Zdroj: (gtk-image1-1.py)
  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       # load an image from file
 22       img = gtk.Image() 
 23       img.set_from_file( "penguin.jpg") 
 24       img.show()
 25       self.mainbox.pack_start( img)
 26       # create a button with an image
 27       b = gtk.Button()
 28       self.img = gtk.Image() 
 29       self.img.set_from_stock( gtk.STOCK_DELETE, gtk.ICON_SIZE_BUTTON) 
 30       b.add( self.img)
 31       self.img.show()
 32       b.show()
 33       b.connect( "clicked", self.toggle_image)
 34       self.mainbox.pack_start( b)
 35       # show the box
 36       self.mainbox.show()
 37       self.counter = 0 # we need this later
 38   
 39     def toggle_image( self, w):
 40       ids = gtk.stock_list_ids() 
 41       stock = ids[ self.counter]
 42       self.img.set_from_stock( stock, gtk.ICON_SIZE_BUTTON) 
 43       if self.counter+1 < len( ids):
 44         self.counter += 1
 45       else:
 46         self.counter = 0
 47   
 48     def main( self):
 49       gtk.main()
 50   
 51     def destroy( self, w):
 52       gtk.main_quit()
 53   
 54   
 55   if __name__ == "__main__":
 56     m = MyGUI( "Image example")
 57     m.main()
Screenshot:
Program screenshot gtk-image1-1.pngProgram screenshot gtk-image1-1a.png
Doba běhu: 4158.2 ms
The following code shows how to query available stock items and dislays a comprehensive overview of available stock items.
Expand/Shrink
Zdroj: (gtk-image1-2.py)
  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( 400, 400)
 14       self.window.connect( "destroy", self.destroy)
 15       self.create_interior()
 16       self.window.show_all()
 17   
 18     def create_interior( self):
 19       top = gtk.ScrolledWindow() 
 20       top.set_policy( gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) 
 21       self.window.add( top) 
 22       self.mainbox = gtk.Table(3,100)
 23       top.add_with_viewport( self.mainbox) 
 24       for row,stock in enumerate( gtk.stock_list_ids()):
 25         img = gtk.Image() 
 26         img.set_from_stock( stock, gtk.ICON_SIZE_BUTTON) 
 27         img.show()
 28         self.mainbox.attach( img, 0, 1, row, row+1)
 29         desc = gtk.stock_lookup( stock)
 30         # for some reason the lookup sometimes fails :(
 31         if desc != None:
 32           stock_id = gtk.Label( desc[0])
 33           stock_id.show()
 34           label = gtk.Label( desc[1])
 35           label.show()
 36           self.mainbox.attach( stock_id, 1, 2, row, row+1)
 37           self.mainbox.attach( label, 2, 3, row, row+1)
 38       # show the box
 39       self.mainbox.show()
 40   
 41     def main( self):
 42       gtk.main()
 43   
 44     def destroy( self, w):
 45       gtk.main_quit()
 46   
 47   
 48   if __name__ == "__main__":
 49     m = MyGUI( "Image example")
 50     m.main()
Screenshot:
Program screenshot gtk-image1-2.png
Doba běhu: 1504.0 ms
Another example of using an Image may be found on page Scales. It demonstrates scaling of an image.