Εικόνα (Python) Marta Maria Casetti mmcasetti@gmail.com 2012 Sindhu S sindhus@live.in 2014 Ένα γραφικό στοιχείο που εμφανίζει μια εικόνα Ελληνική μεταφραστική ομάδα GNOME team@gnome.gr 2012-2015 Δημήτρης Σπίγγος dmtrs32@gmail.com 2012, 2013 Μαρία Θουκιδίδου marablack3@gmail.com 2014 Θάνος Τρυφωνίδης tomtryf@gmail.com 2014, 2015 Εικόνα

Αυτή η GtkApplication εμφανίζει ένα αρχείο εικόνας από τον τρέχοντα κατάλογο.

Εάν το αρχείο εικόνας δεν φορτωθεί επιτυχώς, η εικόνα θα περιέχει ένα εικονίδιο "σπασμένης εικόνας". Το filename.png χρειάζεται να είναι στον τρέχοντα κατάλογο για να δουλέψει αυτός ο κώδικας.

Ο χρησιμοποιούμενος κώδικας για παραγωγή αυτού παραδείγματος 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)

Ένας άλλος τρόπος για να πάρετε ότι έχουμε στο παράδειγμα είναι η δημιουργία της εικόνας ως ένα στιγμιότυπο μιας άλλης κλάσης και προσθήκη του στο στιγμιότυπο του MyWindow στη μέθοδο do_activate(self):

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

Για να χρησιμοποιήσετε αυτό το τμήμα κώδικα, θα χρειαστεί να προσθέσετε τον κώδικα που εισάγει τα Gtk και GdkPixbuf από το gi.repository και τις γραμμές που αντιπροσωπεύουν το παράθυρο MyApplication.

Χρήσιμες μέθοδοι για γραφικό στοιχείο εικόνας

Για φόρτωση εικόνας μέσω δικτύου χρησιμοποιήστε set_from_pixbuf(pixbuf), όπου pixbuf είναι 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)

Εάν preserve_aspect_ratio=True μπορούμε να χρησιμοποιήσουμε new_from_file_at_size(filename, width, height). Εάν width ή height είναι -1, δεν περιορίζεται.

Για φόρτωση από ροή εισόδου, δείτε new_from_stream() και new_from_stream_at_scale() στην τεκμηρίωση

Αναφορές API

Σε αυτό το παράδειγμα χρησιμοποιήσαμε τα παρακάτω:

GtkImage

GtkWindow