<?xml version="1.0" encoding="utf-8"?>
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="image.py" xml:lang="es">
<info>
<title type="text">Image (Python)</title>
<link type="guide" xref="beginner.py#display-widgets"/>
<link type="seealso" xref="properties.py"/>
<link type="next" xref="strings.py"/>
<revision version="0.2" date="2012-06-14" status="draft"/>
<credit type="author copyright">
<name>Marta Maria Casetti</name>
<email its:translate="no">mmcasetti@gmail.com</email>
<years>2012</years>
</credit>
<credit type="editor">
<name>Sindhu S</name>
<email its:translate="no">sindhus@live.in</email>
<years>2014</years>
</credit>
<desc>Un widget que muestra una imagen</desc>
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
<mal:name>Daniel Mustieles</mal:name>
<mal:email>daniel.mustieles@gmail.com</mal:email>
<mal:years>2011 - 2017</mal:years>
</mal:credit>
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
<mal:name>Nicolás Satragno</mal:name>
<mal:email>nsatragno@gmail.com</mal:email>
<mal:years>2012 - 2013</mal:years>
</mal:credit>
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
<mal:name>Jorge González</mal:name>
<mal:email>jorgegonz@svn.gnome.org</mal:email>
<mal:years>2011</mal:years>
</mal:credit>
</info>
<title>Image</title>
<media type="image" mime="image/png" src="media/image.png"/>
<p>Esta GtkApplication muestra una archivo de imagen de la carpeta actual.</p>
<note><p>Si el archivo de imagen no se carga correctamente, la imagen tendrá un icono de «imagen rota». El archivo <file>nombre_archivo.png</file> debe estar en la carpeta actual para que este código funcione.</p></note>
<links type="section"/>
<section id="code">
<title>Código usado para generar este ejemplo</title>
<code mime="text/x-python" style="numbered">
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)
</code>
<p>Otra manera de obtener lo que se tiene en el ejemplo es crear la imagen como una instancia de otra clase y añadírsela a la instancia de <code>MyWindow</code> en el método <code>do_activate(self)</code>:</p>
<code mime="text/x-python">
# 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()
</code>
<note>
<p>Para usar este fragmento de código necesitará aádir el código que importa <code>Gtk</code> y <code>GdkPixbuf</code> desde <code>gi.repository</code> y las líneas que crean una instancia de la ventana <code>MyApplication</code>.</p>
</note>
</section>
<section id="methods">
<title>Métodos útiles para un widget «Image»</title>
<list>
<item>
<p>Para cargar una imagen por red, use <code>set_from_pixbuf(pixbuf)</code>, donde <code>pixbuf</code> es un <link href="http://developer.gnome.org/gdk-pixbuf/unstable//index.html"> GdkPixbuf</link>.</p>
<code mime="text/python">
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)
</code>
<p>Si <code>preserve_aspect_ratio=True</code> puede usar <code>new_from_file_at_size(nombre_archivo, anchura, altura)</code>. Si <code>anchura</code> o <code>altura</code> son <code>-1</code>, no se restringen.</p>
<p>Para cargar de un flujo de entrada, consulte <code>new_from_stream()</code> y <code>new_from_stream_at_scale()</code> en la documentación.</p>
</item>
</list>
</section>
<section id="references">
<title>Referencias de la API</title>
<p>En este ejemplo se usa lo siguiente:</p>
<list>
<item>
<p><link href="https://developer.gnome.org/gtk3/unstable/GtkImage.html"> GtkImage</link></p>
</item>
<item>
<p><link href="https://developer.gnome.org/gtk3/unstable/GtkWindow.html"> GtkWindow</link></p>
</item>
</list>
</section>
</page>