Gtk.Image

g Atk.ImplementorIface Atk.ImplementorIface Gtk.Widget Gtk.Widget Atk.ImplementorIface->Gtk.Widget GObject.GInterface GObject.GInterface GObject.GInterface->Atk.ImplementorIface Gtk.Buildable Gtk.Buildable GObject.GInterface->Gtk.Buildable GObject.InitiallyUnowned GObject.InitiallyUnowned GObject.InitiallyUnowned->Gtk.Widget GObject.Object GObject.Object GObject.Object->GObject.InitiallyUnowned Gtk.Buildable->Gtk.Widget Gtk.Image Gtk.Image Gtk.Misc Gtk.Misc Gtk.Misc->Gtk.Image Gtk.Widget->Gtk.Misc

Example

../_images/Image.png
Subclasses:None

Methods

Inherited:Gtk.Misc (4), Gtk.Widget (278), GObject.Object (37), Gtk.Buildable (10)
Structs:Gtk.WidgetClass (12), GObject.ObjectClass (5)
class new ()
class new_from_animation (animation)
class new_from_file (filename)
class new_from_gicon (icon, size)
class new_from_icon_name (icon_name, size)
class new_from_icon_set (icon_set, size)
class new_from_pixbuf (pixbuf)
class new_from_resource (resource_path)
class new_from_stock (stock_id, size)
class new_from_surface (surface)
  clear ()
  get_animation ()
  get_gicon ()
  get_icon_name ()
  get_icon_set ()
  get_pixbuf ()
  get_pixel_size ()
  get_stock ()
  get_storage_type ()
  set_from_animation (animation)
  set_from_file (filename)
  set_from_gicon (icon, size)
  set_from_icon_name (icon_name, size)
  set_from_icon_set (icon_set, size)
  set_from_pixbuf (pixbuf)
  set_from_resource (resource_path)
  set_from_stock (stock_id, size)
  set_from_surface (surface)
  set_pixel_size (pixel_size)

Properties

Inherited:Gtk.Misc (4), Gtk.Widget (39)
Name Type Flags Short Description
file str r/w Filename to load and display
gicon Gio.Icon r/w The Gio.Icon being displayed
icon-name str r/w The name of the icon from the icon theme
icon-set Gtk.IconSet d/r/w Icon set to display deprecated
icon-size int r/w/en Symbolic size to use for stock icon, icon set or named icon
pixbuf GdkPixbuf.Pixbuf r/w A GdkPixbuf.Pixbuf to display
pixbuf-animation GdkPixbuf.PixbufAnimation r/w GdkPixbuf.PixbufAnimation to display
pixel-size int r/w/en Pixel size to use for named icon
resource str r/w The resource path being displayed
stock str d/r/w Stock ID for a stock image to display deprecated
storage-type Gtk.ImageType r The representation being used for image data
surface cairo.Surface r/w A cairo.Surface to display
use-fallback bool r/w/en Whether to use icon names fallback

Style Properties

Inherited:Gtk.Widget (17)

Fields

Inherited:Gtk.Widget (69), GObject.Object (1)
Name Type Access Description
misc Gtk.Misc r  

Class Details

class Gtk.Image(**kwargs)
Bases:Gtk.Misc
Abstract:No
Structure:Gtk.ImageClass

The Gtk.Image widget displays an image. Various kinds of object can be displayed as an image; most typically, you would load a GdkPixbuf.Pixbuf (“pixel buffer”) from a file, and then display that. There’s a convenience function to do this, Gtk.Image.new_from_file(), used as follows:

GtkWidget *image;
image = gtk_image_new_from_file ("myfile.png");

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.Pixbuf.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 (GdkPixbuf.PixbufAnimation) 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.
static gboolean
button_press_callback (GtkWidget      *event_box,
                       GdkEventButton *event,
                       gpointer        data)
{
  g_print ("Event box clicked at coordinates %f,%f\n",
           event->x, event->y);

  // Returning TRUE means we handled the event, so the signal
  // emission should be stopped (don’t call any further callbacks
  // that may be connected). Return FALSE to continue invoking callbacks.
  return TRUE;
}

static GtkWidget*
create_image (void)
{
  GtkWidget *image;
  GtkWidget *event_box;

  image = gtk_image_new_from_file ("myfile.png");

  event_box = gtk_event_box_new ();

  gtk_container_add (GTK_CONTAINER (event_box), image);

  g_signal_connect (G_OBJECT (event_box),
                    "button_press_event",
                    G_CALLBACK (button_press_callback),
                    image);

  return image;
}

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 Gtk.Misc). 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.Pixbuf using GdkPixbuf.Pixbuf.new_from_inline().

CSS nodes

Gtk.Image has a single CSS node with the name image.

classmethod new()[source]
Returns:a newly created Gtk.Image widget.
Return type:Gtk.Widget

Creates a new empty Gtk.Image widget.

classmethod new_from_animation(animation)[source]
Parameters:animation (GdkPixbuf.PixbufAnimation) – an animation
Returns:a new Gtk.Image widget
Return type:Gtk.Widget

Creates a Gtk.Image displaying the given animation. The Gtk.Image does not assume a reference to the animation; 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 with GLib.PRIORITY_DEFAULT. When using animations to indicate busyness, keep in mind that the animation will only be shown if the main loop is not busy with something that has a higher priority.

classmethod new_from_file(filename)[source]
Parameters:filename (str) – a filename
Returns:a new Gtk.Image
Return type:Gtk.Widget

Creates a new Gtk.Image displaying the file filename. If the file isn’t found or can’t be loaded, the resulting Gtk.Image will display 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 an animation.

If you need to detect failures to load the file, use GdkPixbuf.Pixbuf.new_from_file() to load the file yourself, then create the Gtk.Image from the pixbuf. (Or for animations, use GdkPixbuf.PixbufAnimation.new_from_file()).

The storage type (Gtk.Image.get_storage_type()) of the returned image is not defined, it will be whatever is appropriate for displaying the file.

classmethod new_from_gicon(icon, size)[source]
Parameters:
Returns:

a new Gtk.Image displaying the themed icon

Return type:

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 be displayed instead. If the current icon theme is changed, the icon will be updated appropriately.

New in version 2.14.

classmethod new_from_icon_name(icon_name, size)[source]
Parameters:
Returns:

a new Gtk.Image displaying the themed icon

Return type:

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 be displayed instead. If the current icon theme is changed, the icon will be updated appropriately.

New in version 2.6.

classmethod new_from_icon_set(icon_set, size)[source]
Parameters:
Returns:

a new Gtk.Image

Return type:

Gtk.Widget

Creates a Gtk.Image displaying an icon set. Sample stock sizes are Gtk.IconSize.MENU, Gtk.IconSize.SMALL_TOOLBAR. Instead of using this function, usually it’s better to create a Gtk.IconFactory, put your icon sets in the icon factory, add the icon factory to the list of default factories with Gtk.IconFactory.add_default(), and then use Gtk.Image.new_from_stock(). This will allow themes to override the icon you ship with your application.

The Gtk.Image does not assume a reference to the icon set; you still need to unref it if you own references. Gtk.Image will add its own reference rather than adopting yours.

Deprecated since version 3.10: Use Gtk.Image.new_from_icon_name() instead.

classmethod new_from_pixbuf(pixbuf)[source]
Parameters:pixbuf (GdkPixbuf.Pixbuf or None) – a GdkPixbuf.Pixbuf, or None
Returns:a new Gtk.Image
Return type:Gtk.Widget

Creates a new Gtk.Image displaying pixbuf. The Gtk.Image does not assume a reference to the pixbuf; 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. The Gtk.Image created will not react to state changes. Should you want that, you should use Gtk.Image.new_from_icon_name().

classmethod new_from_resource(resource_path)[source]
Parameters:resource_path (str) – a resource path
Returns:a new Gtk.Image
Return type:Gtk.Widget

Creates a new Gtk.Image displaying the resource file resource_path. If the file isn’t found or can’t be loaded, the resulting Gtk.Image will display 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 an animation.

If you need to detect failures to load the file, use GdkPixbuf.Pixbuf.new_from_file() to load the file yourself, then create the Gtk.Image from the pixbuf. (Or for animations, use GdkPixbuf.PixbufAnimation.new_from_file()).

The storage type (Gtk.Image.get_storage_type()) of the returned image is not defined, it will be whatever is appropriate for displaying the file.

New in version 3.4.

classmethod new_from_stock(stock_id, size)[source]
Parameters:
Returns:

a new Gtk.Image displaying the stock icon

Return type:

Gtk.Widget

Creates a Gtk.Image displaying a stock icon. Sample stock icon names are Gtk.STOCK_OPEN, Gtk.STOCK_QUIT. Sample stock sizes are Gtk.IconSize.MENU, Gtk.IconSize.SMALL_TOOLBAR. If the stock icon name isn’t known, the image will be empty. You can register your own stock icon names, see Gtk.IconFactory.add_default() and Gtk.IconFactory.add().

Deprecated since version 3.10: Use Gtk.Image.new_from_icon_name() instead.

classmethod new_from_surface(surface)[source]
Parameters:surface (cairo.Surface or None) – a cairo.Surface, or None
Returns:a new Gtk.Image
Return type:Gtk.Widget

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

New in version 3.10.

clear()[source]

Resets the image to be empty.

New in version 2.8.

get_animation()[source]
Returns:the displayed animation, or None if the image is empty
Return type:GdkPixbuf.PixbufAnimation or None

Gets the GdkPixbuf.PixbufAnimation being displayed by the Gtk.Image. The storage type of the image must be Gtk.ImageType.EMPTY or Gtk.ImageType.ANIMATION (see Gtk.Image.get_storage_type()). The caller of this function does not own a reference to the returned animation.

get_gicon()[source]
Returns:
gicon:place to store a Gio.Icon, or None
size:place to store an icon size (Gtk.IconSize), or None
Return type:(gicon: Gio.Icon, size: int)

Gets the Gio.Icon and size being displayed by the Gtk.Image. The storage type of the image must be Gtk.ImageType.EMPTY or Gtk.ImageType.GICON (see Gtk.Image.get_storage_type()). The caller of this function does not own a reference to the returned Gio.Icon.

New in version 2.14.

get_icon_name()[source]
Returns:
icon_name:place to store an icon name, or None
size:place to store an icon size (Gtk.IconSize), or None
Return type:(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.ImageType.EMPTY or Gtk.ImageType.ICON_NAME (see Gtk.Image.get_storage_type()). The returned string is owned by the Gtk.Image and should not be freed.

New in version 2.6.

get_icon_set()[source]
Returns:
icon_set:location to store a Gtk.IconSet, or None
size:location to store a stock icon size (Gtk.IconSize), or None
Return type:(icon_set: Gtk.IconSet, size: int)

Gets the icon set and size being displayed by the Gtk.Image. The storage type of the image must be Gtk.ImageType.EMPTY or Gtk.ImageType.ICON_SET (see Gtk.Image.get_storage_type()).

Deprecated since version 3.10: Use Gtk.Image.get_icon_name() instead.

get_pixbuf()[source]
Returns:the displayed pixbuf, or None if the image is empty
Return type:GdkPixbuf.Pixbuf or None

Gets the GdkPixbuf.Pixbuf being displayed by the Gtk.Image. The storage type of the image must be Gtk.ImageType.EMPTY or Gtk.ImageType.PIXBUF (see Gtk.Image.get_storage_type()). The caller of this function does not own a reference to the returned pixbuf.

get_pixel_size()[source]
Returns:the pixel size used for named icons.
Return type:int

Gets the pixel size used for named icons.

New in version 2.6.

get_stock()[source]
Returns:
stock_id:place to store a stock icon name, or None
size:place to store a stock icon size (Gtk.IconSize), or None
Return type:(stock_id: str, size: int)

Gets the stock icon name and size being displayed by the Gtk.Image. The storage type of the image must be Gtk.ImageType.EMPTY or Gtk.ImageType.STOCK (see Gtk.Image.get_storage_type()). The returned string is owned by the Gtk.Image and should not be freed.

Deprecated since version 3.10: Use Gtk.Image.get_icon_name() instead.

get_storage_type()[source]
Returns:image representation being used
Return type:Gtk.ImageType

Gets the type of representation being used by the Gtk.Image to store image data. If the Gtk.Image has no image data, the return value will be Gtk.ImageType.EMPTY.

set_from_animation(animation)[source]
Parameters:animation (GdkPixbuf.PixbufAnimation) – the GdkPixbuf.PixbufAnimation

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

set_from_file(filename)[source]
Parameters:filename (str or None) – a filename or None

See Gtk.Image.new_from_file() for details.

set_from_gicon(icon, size)[source]
Parameters:

See Gtk.Image.new_from_gicon() for details.

New in version 2.14.

set_from_icon_name(icon_name, size)[source]
Parameters:

See Gtk.Image.new_from_icon_name() for details.

New in version 2.6.

set_from_icon_set(icon_set, size)[source]
Parameters:

See Gtk.Image.new_from_icon_set() for details.

Deprecated since version 3.10: Use Gtk.Image.set_from_icon_name() instead.

set_from_pixbuf(pixbuf)[source]
Parameters:pixbuf (GdkPixbuf.Pixbuf or None) – a GdkPixbuf.Pixbuf or None

See Gtk.Image.new_from_pixbuf() for details.

set_from_resource(resource_path)[source]
Parameters:resource_path (str or None) – a resource path or None

See Gtk.Image.new_from_resource() for details.

set_from_stock(stock_id, size)[source]
Parameters:

See Gtk.Image.new_from_stock() for details.

Deprecated since version 3.10: Use Gtk.Image.set_from_icon_name() instead.

set_from_surface(surface)[source]
Parameters:surface (cairo.Surface or None) – a cairo.Surface or None

See Gtk.Image.new_from_surface() for details.

New in version 3.10.

set_pixel_size(pixel_size)[source]
Parameters:pixel_size (int) – the new pixel size

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

New in version 2.6.

Property Details

Gtk.Image.props.file
Name:file
Type:str
Default Value:None
Flags:READABLE, WRITABLE

Filename to load and display

Gtk.Image.props.gicon
Name:gicon
Type:Gio.Icon
Default Value:None
Flags:READABLE, WRITABLE

The Gio.Icon displayed in the Gtk.Image. For themed icons, If the icon theme is changed, the image will be updated automatically.

New in version 2.14.

Gtk.Image.props.icon_name
Name:icon-name
Type:str
Default Value:None
Flags:READABLE, WRITABLE

The name of the icon in the icon theme. If the icon theme is changed, the image will be updated automatically.

New in version 2.6.

Gtk.Image.props.icon_set
Name:icon-set
Type:Gtk.IconSet
Default Value:None
Flags:DEPRECATED, READABLE, WRITABLE

Icon set to display

Deprecated since version 3.10: Use Gtk.Image :icon-name instead.

Gtk.Image.props.icon_size
Name:icon-size
Type:int
Default Value:4
Flags:READABLE, WRITABLE, EXPLICIT_NOTIFY

Symbolic size to use for stock icon, icon set or named icon

Gtk.Image.props.pixbuf
Name:pixbuf
Type:GdkPixbuf.Pixbuf
Default Value:None
Flags:READABLE, WRITABLE

A GdkPixbuf.Pixbuf to display

Gtk.Image.props.pixbuf_animation
Name:pixbuf-animation
Type:GdkPixbuf.PixbufAnimation
Default Value:None
Flags:READABLE, WRITABLE

GdkPixbuf.PixbufAnimation to display

Gtk.Image.props.pixel_size
Name:pixel-size
Type:int
Default Value:-1
Flags:READABLE, WRITABLE, EXPLICIT_NOTIFY

The “pixel-size” property can be used to specify a fixed size overriding the Gtk.Image :icon-size property for images of type Gtk.ImageType.ICON_NAME.

New in version 2.6.

Gtk.Image.props.resource
Name:resource
Type:str
Default Value:None
Flags:READABLE, WRITABLE

A path to a resource file to display.

New in version 3.8.

Gtk.Image.props.stock
Name:stock
Type:str
Default Value:None
Flags:DEPRECATED, READABLE, WRITABLE

Stock ID for a stock image to display

Deprecated since version 3.10: Use Gtk.Image :icon-name instead.

Gtk.Image.props.storage_type
Name:storage-type
Type:Gtk.ImageType
Default Value:Gtk.ImageType.EMPTY
Flags:READABLE

The representation being used for image data

Gtk.Image.props.surface
Name:surface
Type:cairo.Surface
Default Value:None
Flags:READABLE, WRITABLE

A cairo.Surface to display

Gtk.Image.props.use_fallback
Name:use-fallback
Type:bool
Default Value:False
Flags:READABLE, WRITABLE, EXPLICIT_NOTIFY

Whether the icon displayed in the Gtk.Image will use standard icon names fallback. The value of this property is only relevant for images of type Gtk.ImageType.ICON_NAME and Gtk.ImageType.GICON.

New in version 3.0.