Gtk.Image


Gtk.Image — A widget displaying an image

Object Hierarchy:

    GObject
    ╰── GInitiallyUnowned
        ╰── Gtk.Widget
            ╰── Gtk.Misc
                ╰── Gtk.Image

Functions:

Description:

The Gtk.Image widget displays an image. Various kinds of object can be displayed as an image; most typically, you would load a Gdk.Pixbuf ("pixel buffer") from a file, and then display that.

There’s a convenience function to do this, Gtk.Image::new_from_file(),

    image = Gtk.Image()
    image.set_from_file("../_resources/gtk.png")
    self.add(image)

If the file isn’t loaded successfully, the image will contain a broken image icon similar to that used in many web browsers.

If you want to handle errors in loading the file yourself, for example by displaying an error message, then load the image with GdkPixbuf.new_from_file(), then create the Gtk.Image with Gtk.Image::new_from_pixbuf().

The image file may contain an animation, if so the Gtk.Image will display an animation (#GdkPixbufAnimation) instead of a static image.

Gtk.Image is a subclass of Gtk.Misc, which implies that you can align it (center, left, right) and add padding to it, using Gtk.Misc methods.

Gtk.Image is a no window widget (has no Gdk.Window of its own), so by default does not receive events. If you want to receive events on the image, such as button clicks, place the image inside a Gtk.EventBox, then connect to the event signals on the event box.

Handling button press events on a Gtk.Image:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class Image(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_title("Image")
        self.connect("destroy", Gtk.main_quit)

        eventbox = Gtk.EventBox()        
        eventbox.connect("button-press-event", self.on_event_press)
        self.add(eventbox)        

        image = Gtk.Image()
        image.set_from_file("../_resources/gtk.png")        
        eventbox.add(image)

    def on_event_press(self, widget, event):
        print('click', widget, event.button, event.time)

window = Image()
window.show_all()

Gtk.main()

When handling events on the event box, keep in mind that coordinates in the image may be different from event box coordinates due to the alignment and padding settings on the image (see GtkMisc).

The simplest way to solve this is to set the alignment to 0.0 (left/top), and set the padding to zero. Then the origin of the image will be the same as the origin of the event box.

Sometimes an application will want to avoid depending on external data files, such as image files. GTK+ comes with a program to avoid this, called gdk-pixbuf-csource. This library allows you to convert an image into a C variable declaration, which can then be loaded into a #GdkPixbuf using GdkPixbuf.new_from_inline().


Function Details:

get_icon_set()

get_icon_set (self) -> icon_set:Gtk.IconSet, size:int

Gtk.Image:get_icon_set has been deprecated since version 3.10 and should not be used in newly-written code. Use Gtk.Image:get_icon_name() instead. Gets the icon set and size being displayed by the Gtk.Image.The storage type of the image must be GTK_IMAGE_EMPTY orGTK_IMAGE_ICON_SET (see Gtk.Image:get_storage_type()).


get_pixbuf()

get_pixbuf (self) -> GdkPixbuf.Pixbuf

Gets the GdkPixbuf being displayed by the Gtk.Image.The storage type of the image must be GTK_IMAGE_EMPTY orGTK_IMAGE_PIXBUF (see Gtk.Image:get_storage_type()).The caller of this function does not own a reference to thereturned pixbuf.

  • Returns: the displayed pixbuf, or None ifthe image is empty.

get_stock()

get_stock (self) -> stock_id:str, size:int

Gtk.Image:get_stock has been deprecated since version 3.10 and should not be used in newly-written code. Use Gtk.Image:get_icon_name() instead. Gets the stock icon name and size being displayed by the Gtk.Image.The storage type of the image must be GTK_IMAGE_EMPTY orGTK_IMAGE_STOCK (see Gtk.Image:get_storage_type()).The returned string is owned by the Gtk.Image and should notbe freed.


get_animation()

get_animation (self) -> GdkPixbuf.PixbufAnimation

Gets the GdkPixbufAnimation being displayed by the Gtk.Image.The storage type of the image must be GTK_IMAGE_EMPTY orGTK_IMAGE_ANIMATION (see Gtk.Image:get_storage_type()).The caller of this function does not own a reference to thereturned animation.

  • Returns: the displayed animation, or None ifthe image is empty.

get_icon_name()

get_icon_name (self) -> icon_name:str, size:int

Gets the icon name and size being displayed by the Gtk.Image.The storage type of the image must be GTK_IMAGE_EMPTY orGTK_IMAGE_ICON_NAME (see Gtk.Image:get_storage_type()).The returned string is owned by the Gtk.Image and should notbe freed.

  • Since: 2.6

get_gicon()

get_gicon (self) -> gicon:Gio.Icon, size:int

Gets the GIcon and size being displayed by the Gtk.Image.The storage type of the image must be GTK_IMAGE_EMPTY orGTK_IMAGE_GICON (see Gtk.Image:get_storage_type()).The caller of this function does not own a reference to thereturned GIcon.

  • Since: 2.14

get_storage_type()

get_storage_type (self) -> Gtk.ImageType

Gets the type of representation being used by the `Gtk.Imagetostore image data. If theGtk.Imagehas no image data,the return value will beGTK_IMAGE_EMPTY`.

  • Returns: image representation being used

new_from_file()

new_from_file (filename:str) -> Gtk.Widget

Creates a new Gtk.Image displaying the file filename. If the fileisn’t found or can’t be loaded, the resulting Gtk.Image willdisplay a “broken image” icon. This function never returns None,it always returns a valid Gtk.Image widget. If the file contains an animation, the image will contain ananimation. If you need to detect failures to load the file, usegdk_pixbuf_new_from_file() to load the file yourself, then createthe Gtk.Image from the pixbuf. (Or for animations, usegdk_pixbuf_animation_new_from_file()). The storage type (Gtk.Image:get_storage_type()) of the returnedimage is not defined, it will be whatever is appropriate fordisplaying the file.

  • Returns: a new Gtk.Image

new_from_icon_set()

new_from_icon_set (icon_set:Gtk.IconSet, size:int) -> Gtk.Widget

Gtk.Image:new_from_icon_set has been deprecated since version 3.10 and should not be used in newly-written code. Use Gtk.Image:new_from_icon_name() instead. Creates a Gtk.Image displaying an icon set. Sample stock sizes areGTK_ICON_SIZE_MENU, GTK_ICON_SIZE_SMALL_TOOLBAR. Instead of usingthis function, usually it’s better to create a Gtk.IconFactory, putyour icon sets in the icon factory, add the icon factory to thelist of default factories with `Gtk.IconFactory:add_default(), andthen useGtk.Image:new_from_stock(). This will allow themes tooverride the icon you ship with your application. TheGtk.Imagedoes not assume a reference to theicon set; you still need to unref it if you own references.Gtk.Image` will add its own reference rather than adopting yours.

  • Returns: a new Gtk.Image

new_from_pixbuf()

new_from_pixbuf (pixbuf:GdkPixbuf.Pixbuf=None) -> Gtk.Widget

Creates a new Gtk.Image displaying pixbuf.The Gtk.Image does not assume a reference to thepixbuf; you still need to unref it if you own references.Gtk.Image will add its own reference rather than adopting yours. Note that this function just creates an Gtk.Image from the pixbuf. TheGtk.Image created will not react to state changes. Should you want that, you should use Gtk.Image:new_from_icon_name().

  • Returns: a new Gtk.Image

new_from_stock()

new_from_stock (stock_id:str, size:int) -> Gtk.Widget

Gtk.Image:new_from_stock has been deprecated since version 3.10 and should not be used in newly-written code. Use Gtk.Image:new_from_icon_name() instead. Creates a Gtk.Image displaying a stock icon. Sample stock iconnames are GTK_STOCK_OPEN, GTK_STOCK_QUIT. Sample stock sizesare GTK_ICON_SIZE_MENU, GTK_ICON_SIZE_SMALL_TOOLBAR. If the stockicon name isn’t known, the image will be empty.You can register your own stock icon names, seeGtk.IconFactory:add_default`()` andGtk.IconFactory:add().

  • Returns: a new Gtk.Image displaying the stock icon

new_from_animation()

new_from_animation (animation:GdkPixbuf.PixbufAnimation) -> Gtk.Widget

Creates a Gtk.Image displaying the given animation.The Gtk.Image does not assume a reference to theanimation; you still need to unref it if you own references.Gtk.Image will add its own reference rather than adopting yours. Note that the animation frames are shown using a timeout withG_PRIORITY_DEFAULT. When using animations to indicate busyness,keep in mind that the animation will only be shown if the main loopis not busy with something that has a higher priority.

  • Returns: a new Gtk.Image widget

new_from_icon_name()

new_from_icon_name (icon_name:str, size:int) -> Gtk.Widget

Creates a Gtk.Image displaying an icon from the current icon theme.If the icon name isn’t known, a “broken image” icon will bedisplayed instead. If the current icon theme is changed, the iconwill be updated appropriately.

  • Returns: a new Gtk.Image displaying the themed icon

  • Since: 2.6


new_from_gicon()

new_from_gicon (icon:Gio.Icon, size:int) -> Gtk.Widget

Creates a Gtk.Image displaying an icon from the current icon theme.If the icon name isn’t known, a “broken image” icon will bedisplayed instead. If the current icon theme is changed, the iconwill be updated appropriately.

  • Returns: a new Gtk.Image displaying the themed icon

  • Since: 2.14


new_from_resource()

new_from_resource (resource_path:str) -> Gtk.Widget

Creates a new Gtk.Image displaying the resource file resource_path. If the fileisn’t found or can’t be loaded, the resulting Gtk.Image willdisplay a “broken image” icon. This function never returns None,it always returns a valid Gtk.Image widget. If the file contains an animation, the image will contain ananimation. If you need to detect failures to load the file, usegdk_pixbuf_new_from_file() to load the file yourself, then createthe Gtk.Image from the pixbuf. (Or for animations, usegdk_pixbuf_animation_new_from_file()). The storage type (Gtk.Image:get_storage_type()) of the returnedimage is not defined, it will be whatever is appropriate fordisplaying the file.

  • Returns: a new Gtk.Image

  • Since: 3.4


new_from_surface()

new_from_surface (surface:cairo.Surface=None) -> Gtk.Widget

Creates a new Gtk.Image displaying surface.The Gtk.Image does not assume a reference to thesurface; you still need to unref it if you own references.Gtk.Image will add its own reference rather than adopting yours.

  • Returns: a new Gtk.Image

  • Since: 3.10


set_from_file()

set_from_file (self, filename:str=None)

See Gtk.Image:new_from_file() for details.


set_from_icon_set()

set_from_icon_set (self, icon_set:Gtk.IconSet, size:int)

Gtk.Image:set_from_icon_set has been deprecated since version 3.10 and should not be used in newly-written code. Use Gtk.Image:set_from_icon_name() instead. See Gtk.Image:new_from_icon_set() for details.


set_from_pixbuf()

set_from_pixbuf (self, pixbuf:GdkPixbuf.Pixbuf=None)

See Gtk.Image:new_from_pixbuf() for details.


set_from_stock()

set_from_stock (self, stock_id:str, size:int)

Gtk.Image:set_from_stock has been deprecated since version 3.10 and should not be used in newly-written code. Use Gtk.Image:set_from_icon_name() instead. See Gtk.Image:new_from_stock() for details.


set_from_animation()

set_from_animation (self, animation:GdkPixbuf.PixbufAnimation)

Causes the Gtk.Image to display the given animation (or displaynothing, if you set the animation to None).


set_from_icon_name()

set_from_icon_name (self, icon_name:str, size:int)

See Gtk.Image:new_from_icon_name() for details.

  • Since: 2.6

set_from_gicon()

set_from_gicon (self, icon:Gio.Icon, size:int)

See Gtk.Image:new_from_gicon() for details.

  • Since: 2.14

set_from_resource()

set_from_resource (self, resource_path:str=None)

See Gtk.Image:new_from_resource() for details.


set_from_surface()

set_from_surface (self, surface:cairo.Surface)

See Gtk.Image:new_from_surface() for details.

  • Since: 3.10

clear()

clear (self)

Resets the image to be empty.

  • Since: 2.8

new()

new () -> Gtk.Widget

Creates a new empty Gtk.Image widget.

  • Returns: a newly created Gtk.Image widget.

set_pixel_size()

set_pixel_size (self, pixel_size:int)

Sets the pixel size to use for named icons. If the pixel size is setto a value != -1, it is used instead of the icon size set byGtk.Image:set_from_icon_name().

  • Since: 2.6

get_pixel_size()

get_pixel_size (self) -> int

Gets the pixel size used for named icons.

  • Returns: the pixel size used for named icons.

  • Since: 2.6


Example:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class Image(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_title("Image")
        self.connect("destroy", Gtk.main_quit)

        image = Gtk.Image()
        image.set_from_file("../_resources/gtk.png")
        self.add(image)

window = Image()
window.show_all()

Gtk.main()

results matching ""

    No results matching ""