|
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="fr">
|
|
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 élément graphique qui affiche une image</desc>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Luc Rebert,</mal:name>
|
|
Packit |
1470ea |
<mal:email>traduc@rebert.name</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011</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>Alain Lojewski,</mal:name>
|
|
Packit |
1470ea |
<mal:email>allomervan@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011-2012</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>Luc Pionchon</mal:name>
|
|
Packit |
1470ea |
<mal:email>pionchon.luc@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011</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>Bruno Brouard</mal:name>
|
|
Packit |
1470ea |
<mal:email>annoa.b@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011-12</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>Luis Menina</mal:name>
|
|
Packit |
1470ea |
<mal:email>liberforce@freeside.fr</mal:email>
|
|
Packit |
1470ea |
<mal:years>2014</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 |
Cette application Gtk affiche un fichier image issu du dossier en cours.
|
|
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 utilisé pour générer cet exemple</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 |
Une autre manière d'obtenir ce qu'il y a dans cet exemple est de créer l'image en tant qu'exemple d'une autre classe et de l'ajouter à MyWindow dans la méthode 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 |
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>Méthodes utiles pour un élément graphique Image</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 |
Si preserve_aspect_ratio=True est vrai, utilisez new_from_file_at_size(nomdufichier, largeur, hauteur) . Si largeur ou hauteur est -1 , il n'y a pas de contrainte.
|
|
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>Références API</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Dans cet exemple, les éléments suivants sont utilisés :
|
|
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>
|