# Image (Python)

Marta Maria Casetti
mmcasetti@gmail.com 2012
Un widget que muestra una imagen

# Image

Esta GtkApplication muestra una archivo de imagen de la carpeta actual.

Si el archivo de imagen no se carga correctamente, la imagen tendrá un icono de «imagen rota». El archivo nombre_archivo.png debe estar en la carpeta actual para que este código funcione.

# Código usado para generar este ejemplo

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()

# Métodos útiles para un widget «Image»

To set a stock icon as image, you can use `set_from_stock(stock_id, size)` where `stock_id` is a stock icon such as `Gtk.STOCK_ABOUT` (more can be found at Stock Items, with the caveat that they should be modified as above) and `size` is a stock icon size to be chosen from `Gtk.IconSize.INVALID, Gtk.IconSize.MENU, Gtk.IconSize.SMALL_TOOLBAR, Gtk.IconSize.LARGE_TOOLBAR, Gtk.IconSize.BUTTON, Gtk.IconSize.DND, Gtk.IconSize.DIALOG`.

You can also use `set_from_icon_name(icon_name, size)`, where `icon_name` is a stock icon name such as `"gtk-about"` (more can be found as above) and `size` is as above.

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

# Referencias de la API

En este ejemplo se usa lo siguiente:

GtkImage

GtkWindow