Image#
Superclasses: Widget
, InitiallyUnowned
, Object
Implemented Interfaces: Accessible
, Buildable
, ConstraintTarget
The GtkImage
widget displays an 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
new_from_file
, for instance:
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
new_from_file
, then create the GtkImage
with
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, resource
,
new_from_resource
, and set_from_resource
should be used.
GtkImage
displays its image as an icon, with a size that is determined
by the application. See 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
icon_size
property.
Accessibility#
GtkImage
uses the GTK_ACCESSIBLE_ROLE_IMG
role.
Constructors#
- class Image
-
- classmethod new_from_file(filename: str) Widget #
Creates a new
GtkImage
displaying the filefilename
.If the file isn’t found or can’t be loaded, the resulting
GtkImage
will display a “broken image” icon. This function never returnsNone
, it always returns a validGtkImage
widget.If you need to detect failures to load the file, use
new_from_file
to load the file yourself, then create theGtkImage
from the texture.The storage type (see
get_storage_type
) of the returned image is not defined, it will be whatever is appropriate for displaying the file.- Parameters:
filename – a filename
- classmethod new_from_gicon(icon: Icon) 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.
- Parameters:
icon – an icon
- classmethod new_from_icon_name(icon_name: str | None = None) 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.
- Parameters:
icon_name – an icon name
- classmethod new_from_paintable(paintable: Paintable | None = None) Widget #
Creates a new
GtkImage
displayingpaintable
.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 thepaintable
and update its size and contents in response to it.- Parameters:
paintable – a
GdkPaintable
- classmethod new_from_pixbuf(pixbuf: Pixbuf | None = None) Widget #
Creates a new
GtkImage
displayingpixbuf
.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
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. TheGtkImage
created will not react to state changes. Should you want that, you should usenew_from_icon_name
.Deprecated since version 4.12: Use
new_from_paintable
andnew_for_pixbuf
instead- Parameters:
pixbuf – a
GdkPixbuf
- classmethod new_from_resource(resource_path: str) Widget #
Creates a new
GtkImage
displaying the resource fileresource_path
.If the file isn’t found or can’t be loaded, the resulting
GtkImage
will display a “broken image” icon. This function never returnsNone
, it always returns a validGtkImage
widget.If you need to detect failures to load the file, use
new_from_file
to load the file yourself, then create theGtkImage
from the pixbuf.The storage type (see
get_storage_type
) of the returned image is not defined, it will be whatever is appropriate for displaying the file.- Parameters:
resource_path – a resource path
Methods#
- class Image
-
- get_gicon() Icon | None #
Gets the
GIcon
being displayed by theGtkImage
.The storage type of the image must be
EMPTY
orGICON
(seeget_storage_type
). The caller of this function does not own a reference to the returnedGIcon
.
- get_icon_name() str | None #
Gets the icon name and size being displayed by the
GtkImage
.The storage type of the image must be
EMPTY
orICON_NAME
(seeget_storage_type
). The returned string is owned by theGtkImage
and should not be freed.
- get_paintable() Paintable | None #
Gets the image
GdkPaintable
being displayed by theGtkImage
.The storage type of the image must be
EMPTY
orPAINTABLE
(seeget_storage_type
). The caller of this function does not own a reference to the returned paintable.
- get_storage_type() 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 beEMPTY
.
- set_from_file(filename: str | None = None) None #
Sets a
GtkImage
to show a file.See
new_from_file
for details.- Parameters:
filename – a filename
- set_from_gicon(icon: Icon) None #
Sets a
GtkImage
to show aGIcon
.See
new_from_gicon
for details.- Parameters:
icon – an icon
- set_from_icon_name(icon_name: str | None = None) None #
Sets a
GtkImage
to show a named icon.See
new_from_icon_name
for details.- Parameters:
icon_name – an icon name
- set_from_paintable(paintable: Paintable | None = None) None #
Sets a
GtkImage
to show aGdkPaintable
.See
new_from_paintable
for details.- Parameters:
paintable – a
GdkPaintable
- set_from_pixbuf(pixbuf: Pixbuf | None = None) None #
Sets a
GtkImage
to show aGdkPixbuf
.See
new_from_pixbuf
for details.Note: This is a helper for
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
set_from_paintable
instead- Parameters:
pixbuf – a
GdkPixbuf
orNULL
- set_from_resource(resource_path: str | None = None) None #
Sets a
GtkImage
to show a resource.See
new_from_resource
for details.- Parameters:
resource_path – a resource path
- set_icon_size(icon_size: IconSize) None #
Suggests an icon size to the theme for named icons.
- Parameters:
icon_size – the new icon size
- set_pixel_size(pixel_size: int) None #
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
set_from_icon_name
.- Parameters:
pixel_size – the new pixel size
Properties#
- class Image