Blame platform-demos/C/image.py.page

Packit 1470ea
Packit 1470ea
Packit 1470ea
      xmlns:its="http://www.w3.org/2005/11/its"
Packit 1470ea
      xmlns:xi="http://www.w3.org/2001/XInclude"
Packit 1470ea
      type="guide" style="task"
Packit 1470ea
      id="image.py">
Packit 1470ea
  <info>
Packit 1470ea
    <title type="text">Image (Python)</title>
Packit 1470ea
    <link type="guide" xref="beginner.py#display-widgets"/>
Packit 1470ea
    <link type="seealso" xref="properties.py" />
Packit 1470ea
    <link type="next" xref="strings.py"/>
Packit 1470ea
    <revision version="0.2" date="2012-06-14" status="draft"/>
Packit 1470ea
Packit 1470ea
    <credit type="author copyright">
Packit 1470ea
      <name>Marta Maria Casetti</name>
Packit 1470ea
      <email its:translate="no">mmcasetti@gmail.com</email>
Packit 1470ea
      <years>2012</years>
Packit 1470ea
    </credit>
Packit 1470ea
    <credit type="editor">
Packit 1470ea
      <name>Sindhu S</name>
Packit 1470ea
      <email its:translate="no">sindhus@live.in</email>
Packit 1470ea
      <years>2014</years>
Packit 1470ea
    </credit>
Packit 1470ea
Packit 1470ea
    <desc>A widget displaying an image</desc>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>Image</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/image.png"/>
Packit 1470ea
  

This GtkApplication displays an image file from the current directory.

Packit 1470ea
Packit 1470ea
  <note>

Packit 1470ea
    If the image file is not loaded successfully, the image will contain a
Packit 1470ea
    "broken image" icon. <file>filename.png</file> needs to be in the
Packit 1470ea
    current directory for this code to work.
Packit 1470ea
  

</note>
Packit 1470ea
Packit 1470ea
  <links type="section"/>
Packit 1470ea
Packit 1470ea
  <section id="code">
Packit 1470ea
  <title>Code used to generate this example</title>
Packit 1470ea
Packit 1470ea
  
Packit 1470ea
  <xi:include href="samples/image.py" parse="text"><xi:fallback/></xi:include>
Packit 1470ea
  
Packit 1470ea
Packit 1470ea
  

Another way to obtain what we have in the example is to create the image as

Packit 1470ea
  an instance of another class and add it to the instance of
Packit 1470ea
  MyWindow in the do_activate(self) method:

Packit 1470ea
Packit 1470ea
  
Packit 1470ea
    # a class to create a window
Packit 1470ea
    class MyWindow(Gtk.ApplicationWindow):
Packit 1470ea
    def __init__(self, app):
Packit 1470ea
        Gtk.Window.__init__(self, title="Welcome to GNOME", application=app)
Packit 1470ea
        self.set_default_size(300, 300)
Packit 1470ea
Packit 1470ea
    # a class to create an image
Packit 1470ea
    class MyImage(Gtk.Image):
Packit 1470ea
    def __init__(self):
Packit 1470ea
        Gtk.Image.__init__(self)
Packit 1470ea
        self.set_from_file("gnome-image.png")
Packit 1470ea
Packit 1470ea
    class MyApplication(Gtk.Application):
Packit 1470ea
    def __init__(self):
Packit 1470ea
        Gtk.Application.__init__(self)
Packit 1470ea
Packit 1470ea
    def do_activate(self):
Packit 1470ea
        # create an instance of MyWindow
Packit 1470ea
        win = MyWindow(self)
Packit 1470ea
        # create an instance of MyImage and add it to the window
Packit 1470ea
        win.add(MyImage())
Packit 1470ea
        # show the window and everything on it
Packit 1470ea
        win.show_all()
Packit 1470ea
  
Packit 1470ea
Packit 1470ea
  <note>
Packit 1470ea
    

To use this code snippet, you will need to add the code that imports

Packit 1470ea
    Gtk and GdkPixbuf from gi.repository
Packit 1470ea
    and lines that instantiate the MyApplication window.

Packit 1470ea
  </note>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="methods">
Packit 1470ea
  <title>Useful methods for an Image widget</title>
Packit 1470ea
Packit 1470ea
  <list>
Packit 1470ea
    <item>
Packit 1470ea
      

To load an image over a network use

Packit 1470ea
      set_from_pixbuf(pixbuf), where pixbuf is a
Packit 1470ea
      <link href="https://developer.gnome.org/gdk-pixbuf/unstable/index.html">
Packit 1470ea
      GdkPixbuf</link>.

Packit 1470ea
      
Packit 1470ea
        from gi.repository import Gtk
Packit 1470ea
        from gi.repository import GdkPixbuf
Packit 1470ea
        import sys
Packit 1470ea
Packit 1470ea
        class MyWindow(Gtk.ApplicationWindow):
Packit 1470ea
            # create a window
Packit 1470ea
            def __init__(self, app):
Packit 1470ea
                Gtk.Window.__init__(self, title="Welcome to GNOME", application=app)
Packit 1470ea
                self.set_default_size(300, 300)
Packit 1470ea
Packit 1470ea
                # create a pixbuf from file filename="gnome-image.png", with width=32
Packit 1470ea
                # and height=64 amd boolean preserve_aspect_ratio=False.
Packit 1470ea
                pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale("gnome-image.png", 64, 128, False)
Packit 1470ea
Packit 1470ea
                # create an image
Packit 1470ea
                image = Gtk.Image()
Packit 1470ea
                # set the content of the image as the pixbuf
Packit 1470ea
                image.set_from_pixbuf(pixbuf)
Packit 1470ea
                # add the image to the window
Packit 1470ea
                self.add(image)
Packit 1470ea
        
Packit 1470ea
        

If preserve_aspect_ratio=True we can use

Packit 1470ea
        new_from_file_at_size(filename, width, height). If
Packit 1470ea
        width or height is -1, it is
Packit 1470ea
        not constrained.

Packit 1470ea
        

For loading from an input stream, see new_from_stream()

Packit 1470ea
        and new_from_stream_at_scale() in the documentation.

Packit 1470ea
    </item>
Packit 1470ea
  </list>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="references">
Packit 1470ea
    <title>API References</title>
Packit 1470ea
Packit 1470ea
    

In this sample we used the following:

Packit 1470ea
    <list>
Packit 1470ea
      <item>
Packit 1470ea
        

<link href="https://developer.gnome.org/gtk3/unstable/GtkImage.html">

Packit 1470ea
        GtkImage</link>

Packit 1470ea
      </item>
Packit 1470ea
      <item>
Packit 1470ea
        

<link href="https://developer.gnome.org/gtk3/unstable/GtkWindow.html">

Packit 1470ea
        GtkWindow</link>

Packit 1470ea
      </item>
Packit 1470ea
    </list>
Packit 1470ea
Packit 1470ea
  </section>
Packit 1470ea
</page>