|
Packit |
1470ea |
|
|
Packit |
1470ea |
<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">
|
|
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>Un widget que muestra una imagen</desc>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Daniel Mustieles</mal:name>
|
|
Packit |
1470ea |
<mal:email>daniel.mustieles@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011 - 2017</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Nicolás Satragno</mal:name>
|
|
Packit |
1470ea |
<mal:email>nsatragno@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2012 - 2013</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Jorge González</mal:name>
|
|
Packit |
1470ea |
<mal:email>jorgegonz@svn.gnome.org</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>Image</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/image.png"/>
|
|
Packit |
1470ea |
Esta GtkApplication muestra una archivo de imagen de la carpeta actual.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<note>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. </note>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<links type="section"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="code">
|
|
Packit |
1470ea |
<title>Código usado para generar este ejemplo</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
from gi.repository import Gtk
|
|
Packit |
1470ea |
import sys
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class MyWindow(Gtk.ApplicationWindow):
|
|
Packit |
1470ea |
# create a window
|
|
Packit |
1470ea |
|
|
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 an image
|
|
Packit |
1470ea |
image = Gtk.Image()
|
|
Packit |
1470ea |
# set the content of the image as the file filename.png
|
|
Packit |
1470ea |
image.set_from_file("gnome-image.png")
|
|
Packit |
1470ea |
# add the image to the window
|
|
Packit |
1470ea |
self.add(image)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class MyApplication(Gtk.Application):
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def __init__(self):
|
|
Packit |
1470ea |
Gtk.Application.__init__(self)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def do_activate(self):
|
|
Packit |
1470ea |
win = MyWindow(self)
|
|
Packit |
1470ea |
win.show_all()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def do_startup(self):
|
|
Packit |
1470ea |
Gtk.Application.do_startup(self)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
app = MyApplication()
|
|
Packit |
1470ea |
exit_status = app.run(sys.argv)
|
|
Packit |
1470ea |
sys.exit(exit_status)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
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 MyWindow en el método do_activate(self) :
|
|
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 |
Para usar este fragmento de código necesitará aádir el código que importa Gtk y GdkPixbuf desde gi.repository y las líneas que crean una instancia de la ventana MyApplication .
|
|
Packit |
1470ea |
</note>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="methods">
|
|
Packit |
1470ea |
<title>Métodos útiles para un widget «Image»</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
Para cargar una imagen por red, use set_from_pixbuf(pixbuf) , donde pixbuf es un <link href="http://developer.gnome.org/gdk-pixbuf/unstable//index.html"> 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 |
Si preserve_aspect_ratio=True puede usar new_from_file_at_size(nombre_archivo, anchura, altura) . Si anchura o altura son -1 , no se restringen.
|
|
Packit |
1470ea |
Para cargar de un flujo de entrada, consulte new_from_stream() y new_from_stream_at_scale() en la documentación.
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="references">
|
|
Packit |
1470ea |
<title>Referencias de la API</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
En este ejemplo se usa lo siguiente:
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<link href="https://developer.gnome.org/gtk3/unstable/GtkImage.html"> GtkImage</link>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<link href="https://developer.gnome.org/gtk3/unstable/GtkWindow.html"> GtkWindow</link>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|