Gtk.Image

g GObject.GInterface GObject.GInterface Gtk.Accessible Gtk.Accessible GObject.GInterface->Gtk.Accessible Gtk.Buildable Gtk.Buildable GObject.GInterface->Gtk.Buildable Gtk.ConstraintTarget Gtk.ConstraintTarget GObject.GInterface->Gtk.ConstraintTarget GObject.InitiallyUnowned GObject.InitiallyUnowned Gtk.Widget Gtk.Widget GObject.InitiallyUnowned->Gtk.Widget GObject.Object GObject.Object GObject.Object->GObject.InitiallyUnowned Gtk.Accessible->Gtk.Widget Gtk.Buildable->Gtk.Widget Gtk.ConstraintTarget->Gtk.Widget Gtk.Image Gtk.Image Gtk.Widget->Gtk.Image

Example

../_images/Image.png
Subclasses:

None

Methods

Inherited:

Gtk.Widget (181), GObject.Object (37), Gtk.Accessible (15), Gtk.Buildable (1)

Structs:

Gtk.WidgetClass (18), GObject.ObjectClass (5)

class

new ()

class

new_from_file (filename)

class

new_from_gicon (icon)

class

new_from_icon_name (icon_name)

class

new_from_paintable (paintable)

class

new_from_pixbuf (pixbuf)

class

new_from_resource (resource_path)

clear ()

get_gicon ()

get_icon_name ()

get_icon_size ()

get_paintable ()

get_pixel_size ()

get_storage_type ()

set_from_file (filename)

set_from_gicon (icon)

set_from_icon_name (icon_name)

set_from_paintable (paintable)

set_from_pixbuf (pixbuf)

set_from_resource (resource_path)

set_icon_size (icon_size)

set_pixel_size (pixel_size)

Virtual Methods

Inherited:

Gtk.Widget (25), GObject.Object (7), Gtk.Accessible (6), Gtk.Buildable (9)

Properties

Inherited:

Gtk.Widget (34), Gtk.Accessible (1)

Name

Type

Flags

Short Description

file

str

r/w

gicon

Gio.Icon

r/w

icon-name

str

r/w

icon-size

Gtk.IconSize

r/w/en

paintable

Gdk.Paintable

r/w

pixel-size

int

r/w/en

resource

str

r/w

storage-type

Gtk.ImageType

r

use-fallback

bool

r/w/en

Signals

Inherited:

Gtk.Widget (13), GObject.Object (1)

Fields

Inherited:

Gtk.Widget (13), GObject.Object (1)

Class Details

class Gtk.Image(**kwargs)
Bases:

Gtk.Widget

Abstract:

No

The GtkImage widget displays an image.

An example Gtk.Image

Various kinds of object can be displayed as an image; most typically, you would load a GdkTexture from a file, using the convenience function [ctor`Gtk`.Image.new_from_file], for instance:

``c GtkWidget *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 [ctor`Gdk`.Texture.new_from_file], then create the GtkImage with [ctor`Gtk`.Image.new_from_paintable].

Sometimes an application will want to avoid depending on external data files, such as image files. See the documentation of GResource inside GIO, for details. In this case, [property`Gtk`.Image:resource], [ctor`Gtk`.Image.new_from_resource], and [method`Gtk`.Image.set_from_resource] should be used.

GtkImage displays its image as an icon, with a size that is determined by the application. See [class`Gtk`.Picture] if you want to show an image at is actual size.

CSS nodes

GtkImage has a single CSS node with the name image. The style classes .normal-icons or .large-icons may appear, depending on the [property`Gtk`.Image:icon-size] property.

Accessibility

GtkImage uses the GTK_ACCESSIBLE_ROLE_IMG role.

classmethod new()[source]
Returns:

a newly created GtkImage widget.

Return type:

Gtk.Widget

Creates a new empty GtkImage widget.

classmethod new_from_file(filename)[source]
Parameters:

filename (str) – a filename

Returns:

a new GtkImage

Return type:

Gtk.Widget

Creates a new GtkImage displaying the file filename.

If the file isn’t found or can’t be loaded, the resulting GtkImage will display a “broken image” icon. This function never returns None, it always returns a valid GtkImage widget.

If you need to detect failures to load the file, use [ctor`Gdk`.Texture.new_from_file] to load the file yourself, then create the GtkImage from the texture.

The storage type (see [method`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)[source]
Parameters:

icon (Gio.Icon) – an icon

Returns:

a new GtkImage displaying the themed icon

Return type:

Gtk.Widget

Creates a GtkImage 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.

classmethod new_from_icon_name(icon_name)[source]
Parameters:

icon_name (str or None) – an icon name

Returns:

a new GtkImage displaying the themed icon

Return type:

Gtk.Widget

Creates a GtkImage 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.

classmethod new_from_paintable(paintable)[source]
Parameters:

paintable (Gdk.Paintable or None) – a GdkPaintable

Returns:

a new GtkImage

Return type:

Gtk.Widget

Creates a new GtkImage displaying paintable.

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

The GtkImage will track changes to the paintable and update its size and contents in response to it.

classmethod new_from_pixbuf(pixbuf)[source]
Parameters:

pixbuf (GdkPixbuf.Pixbuf or None) – a GdkPixbuf

Returns:

a new GtkImage

Return type:

Gtk.Widget

Creates a new GtkImage displaying pixbuf.

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

This is a helper for [ctor`Gtk`.Image.new_from_paintable], and you can’t get back the exact pixbuf once this is called, only a texture.

Note that this function just creates an GtkImage from the pixbuf. The GtkImage created will not react to state changes. Should you want that, you should use [ctor`Gtk`.Image.new_from_icon_name].

Deprecated since version 4.12: Use [ctor`Gtk`.Image.new_from_paintable] and [ctor`Gdk`.Texture.new_for_pixbuf] instead

classmethod new_from_resource(resource_path)[source]
Parameters:

resource_path (str) – a resource path

Returns:

a new GtkImage

Return type:

Gtk.Widget

Creates a new GtkImage displaying the resource file resource_path.

If the file isn’t found or can’t be loaded, the resulting GtkImage will display a “broken image” icon. This function never returns None, it always returns a valid GtkImage widget.

If you need to detect failures to load the file, use [ctor`GdkPixbuf`.Pixbuf.new_from_file] to load the file yourself, then create the GtkImage from the pixbuf.

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

clear()[source]

Resets the image to be empty.

get_gicon()[source]
Returns:

a GIcon

Return type:

Gio.Icon or None

Gets the GIcon being displayed by the GtkImage.

The storage type of the image must be Gtk.ImageType.EMPTY or Gtk.ImageType.GICON (see [method`Gtk`.Image.get_storage_type]). The caller of this function does not own a reference to the returned GIcon.

get_icon_name()[source]
Returns:

the icon name

Return type:

str or None

Gets the icon name and size being displayed by the GtkImage.

The storage type of the image must be Gtk.ImageType.EMPTY or Gtk.ImageType.ICON_NAME (see [method`Gtk`.Image.get_storage_type]). The returned string is owned by the GtkImage and should not be freed.

get_icon_size()[source]
Returns:

the image size used by icons

Return type:

Gtk.IconSize

Gets the icon size used by the self when rendering icons.

get_paintable()[source]
Returns:

the displayed paintable

Return type:

Gdk.Paintable or None

Gets the image GdkPaintable being displayed by the GtkImage.

The storage type of the image must be Gtk.ImageType.EMPTY or Gtk.ImageType.PAINTABLE (see [method`Gtk`.Image.get_storage_type]). The caller of this function does not own a reference to the returned paintable.

get_pixel_size()[source]
Returns:

the pixel size used for named icons.

Return type:

int

Gets the pixel size used for named icons.

get_storage_type()[source]
Returns:

image representation being used

Return type:

Gtk.ImageType

Gets the type of representation being used by the GtkImage to store image data.

If the GtkImage has no image data, the return value will be Gtk.ImageType.EMPTY.

set_from_file(filename)[source]
Parameters:

filename (str or None) – a filename

Sets a GtkImage to show a file.

See [ctor`Gtk`.Image.new_from_file] for details.

set_from_gicon(icon)[source]
Parameters:

icon (Gio.Icon) – an icon

Sets a GtkImage to show a GIcon.

See [ctor`Gtk`.Image.new_from_gicon] for details.

set_from_icon_name(icon_name)[source]
Parameters:

icon_name (str or None) – an icon name

Sets a GtkImage to show a named icon.

See [ctor`Gtk`.Image.new_from_icon_name] for details.

set_from_paintable(paintable)[source]
Parameters:

paintable (Gdk.Paintable or None) – a GdkPaintable

Sets a GtkImage to show a GdkPaintable.

See [ctor`Gtk`.Image.new_from_paintable] for details.

set_from_pixbuf(pixbuf)[source]
Parameters:

pixbuf (GdkPixbuf.Pixbuf or None) – a GdkPixbuf or NULL

Sets a GtkImage to show a GdkPixbuf.

See [ctor`Gtk`.Image.new_from_pixbuf] for details.

Note: This is a helper for [method`Gtk`.Image.set_from_paintable], and you can’t get back the exact pixbuf once this is called, only a paintable.

Deprecated since version 4.12: Use [method`Gtk`.Image.set_from_paintable] instead

set_from_resource(resource_path)[source]
Parameters:

resource_path (str or None) – a resource path

Sets a GtkImage to show a resource.

See [ctor`Gtk`.Image.new_from_resource] for details.

set_icon_size(icon_size)[source]
Parameters:

icon_size (Gtk.IconSize) – the new icon size

Suggests an icon size to the theme for named icons.

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 [method`Gtk`.Image.set_from_icon_name].

Property Details

Gtk.Image.props.file
Name:

file

Type:

str

Default Value:

None

Flags:

READABLE, WRITABLE

The GFile to display.

Gtk.Image.props.gicon
Name:

gicon

Type:

Gio.Icon

Default Value:

None

Flags:

READABLE, WRITABLE

The GIcon displayed in the Gtk.Image.

For themed icons, If the icon theme is changed, the image will be updated automatically.

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.

Gtk.Image.props.icon_size
Name:

icon-size

Type:

Gtk.IconSize

Default Value:

Gtk.IconSize.INHERIT

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

The symbolic size to display icons at.

Gtk.Image.props.paintable
Name:

paintable

Type:

Gdk.Paintable

Default Value:

None

Flags:

READABLE, WRITABLE

The GdkPaintable to display.

Gtk.Image.props.pixel_size
Name:

pixel-size

Type:

int

Default Value:

-1

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

The size in pixels to display icons at.

If set to a value != -1, this property overrides the [property`Gtk`.Image:icon-size] property for images of type GTK_IMAGE_ICON_NAME.

Gtk.Image.props.resource
Name:

resource

Type:

str

Default Value:

None

Flags:

READABLE, WRITABLE

A path to a resource file to display.

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.use_fallback
Name:

use-fallback

Type:

bool

Default Value:

False

Flags:

READABLE, WRITABLE, EXPLICIT_NOTIFY

Whether the icon displayed in the GtkImage 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.