# Image (Python)

Marta Maria Casetti
mmcasetti@gmail.com 2012
Sindhu S
sindhus@live.in 2014
A widget displaying an image
Fran Dieguez
frandieguez@gnome.org 2012-2013.

# Imaxe

This GtkApplication displays an image file from the current directory.

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

# Código usado para xerar este exemplo

from gi.repository import Gtk import sys

class MyWindow(Gtk.ApplicationWindow): # create a window

def __init__(self, app): Gtk.Window.__init__(self, title="Welcome to GNOME", application=app) self.set_default_size(300, 300)

# create an image image = Gtk.Image() # set the content of the image as the file filename.png image.set_from_file("gnome-image.png") # add the image to the window self.add(image)

class MyApplication(Gtk.Application):

def __init__(self): Gtk.Application.__init__(self)

def do_activate(self): win = MyWindow(self) win.show_all()

def do_startup(self): Gtk.Application.do_startup(self)

app = MyApplication() exit_status = app.run(sys.argv) sys.exit(exit_status)

Another way to obtain what we have in the example is to create the image as an instance of another class and add it to the instance of `MyWindow` in the `do_activate(self)` method:

# a class to create a window class MyWindow(Gtk.ApplicationWindow): def __init__(self, app): Gtk.Window.__init__(self, title="Welcome to GNOME", application=app) self.set_default_size(300, 300)

# a class to create an image class MyImage(Gtk.Image): def __init__(self): Gtk.Image.__init__(self) self.set_from_file("gnome-image.png")

class MyApplication(Gtk.Application): def __init__(self): Gtk.Application.__init__(self)

def do_activate(self): # create an instance of MyWindow win = MyWindow(self) # create an instance of MyImage and add it to the window win.add(MyImage()) # show the window and everything on it win.show_all()

To use this code snippet, you will need to add the code that imports `Gtk` and `GdkPixbuf` from `gi.repository` and lines that instantiate the `MyApplication` window.

# Useful methods for an Image widget

To load an image over a network use `set_from_pixbuf(pixbuf)`, where `pixbuf` is a GdkPixbuf.

from gi.repository import Gtk from gi.repository import GdkPixbuf import sys

class MyWindow(Gtk.ApplicationWindow): # create a window def __init__(self, app): Gtk.Window.__init__(self, title="Welcome to GNOME", application=app) self.set_default_size(300, 300)

# create a pixbuf from file filename="gnome-image.png", with width=32 # and height=64 amd boolean preserve_aspect_ratio=False. pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale("gnome-image.png", 64, 128, False)

# create an image image = Gtk.Image() # set the content of the image as the pixbuf image.set_from_pixbuf(pixbuf) # add the image to the window self.add(image)

If `preserve_aspect_ratio=True` we can use `new_from_file_at_size(filename, width, height)`. If `width` or `height` is `-1`, it is not constrained.

For loading from an input stream, see `new_from_stream()` and `new_from_stream_at_scale()` in the documentation.

# API References

Neste exemplo empregaremos o seguinte:

GtkImage

GtkWindow