Gtk.Picture¶
class — extends Widget, Accessible, Buildable, ConstraintTarget
Displays a GdkPaintable.
<picture> <source srcset="picture-dark.png" media="(prefers-color-scheme: dark)"> <img alt="An example GtkPicture" src="picture.png"> </picture>
Many convenience functions are provided to make pictures simple to use. For example, if you want to load an image from a file, and then display it, there’s a convenience function to do this:
If the file isn’t loaded successfully, the picture 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
and image loading framework such as libglycin, then create the GtkPicture
with Picture.new_for_paintable.
Sometimes an application will want to avoid depending on external data
files, such as image files. See the documentation of GResource for details.
In this case, Picture.new_for_resource and
Picture.set_resource should be used.
GtkPicture displays an image at its natural size. See Image
if you want to display a fixed-size image, such as an icon.
Sizing the paintable¶
You can influence how the paintable is displayed inside the GtkPicture
by changing Picture.content-fit. See ContentFit
for details. Picture.can-shrink can be unset to make sure
that paintables are never made smaller than their ideal size - but
be careful if you do not know the size of the paintable in use (like
when displaying user-loaded images). This can easily cause the picture to
grow larger than the screen. And Widget.halign and
Widget.valign can be used to make sure the paintable doesn't
fill all available space but is instead displayed at its original size.
CSS nodes¶
GtkPicture has a single CSS node with the name picture.
Accessibility¶
GtkPicture uses the AccessibleRole.img role.
Constructors¶
new¶
Creates a new empty GtkPicture widget.
new_for_file¶
Creates a new GtkPicture displaying the given file.
If the file isn’t found or can’t be loaded, the resulting
GtkPicture is empty.
If you need to detect failures to load the file, use an
image loading framework such as libglycin to load the file
yourself, then create the GtkPicture from the texture.
Parameters:
file— aGFile
new_for_filename¶
@classmethod
def new_for_filename(cls, filename: str | bytes | os.PathLike[str] | os.PathLike[bytes] | None = ...) -> Widget
Creates a new GtkPicture displaying the file filename.
This is a utility function that calls Picture.new_for_file.
See that function for details.
Parameters:
filename— a filename
new_for_paintable¶
Creates a new GtkPicture displaying paintable.
The GtkPicture will track changes to the paintable and update
its size and contents in response to it.
Parameters:
paintable— aGdkPaintable
new_for_pixbuf¶
:::warning Deprecated since 4.12 This API is deprecated. :::
Creates a new GtkPicture displaying pixbuf.
This is a utility function that calls Picture.new_for_paintable,
See that function for details.
The pixbuf must not be modified after passing it to this function.
Parameters:
pixbuf— aGdkPixbuf
new_for_resource¶
Creates a new GtkPicture displaying the resource at resource_path.
This is a utility function that calls Picture.new_for_file.
See that function for details.
Parameters:
resource_path— resource path to play back
Methods¶
get_alternative_text¶
Gets the alternative textual description of the picture.
The returned string will be None if the picture cannot be described textually.
get_can_shrink¶
Returns whether the GtkPicture respects its contents size.
get_content_fit¶
Returns the fit mode for the content of the GtkPicture.
See ContentFit for details.
get_file¶
Gets the GFile currently displayed if self is displaying a file.
If self is not displaying a file, for example when
Picture.set_paintable was used, then None is returned.
get_isolate_contents¶
Returns whether the contents are isolated.
get_keep_aspect_ratio¶
:::warning Deprecated since 4.8 This API is deprecated. :::
Returns whether the GtkPicture preserves its contents aspect ratio.
get_paintable¶
Gets the GdkPaintable being displayed by the GtkPicture.
set_alternative_text¶
Sets an alternative textual description for the picture contents.
It is equivalent to the "alt" attribute for images on websites.
This text will be made available to accessibility tools.
If the picture cannot be described textually, set this property to None.
Parameters:
alternative_text— a textual description of the contents
set_can_shrink¶
If set to True, then self can be made smaller than its contents.
The contents will then be scaled down when rendering.
If you want to still force a minimum size manually, consider using
Widget.set_size_request.
Also of note is that a similar function for growing does not exist
because the grow behavior can be controlled via
Widget.set_halign and Widget.set_valign.
Parameters:
can_shrink— ifselfcan be made smaller than its contents
set_content_fit¶
Sets how the content should be resized to fit the GtkPicture.
See ContentFit for details.
Parameters:
content_fit— the content fit mode
set_file¶
Makes self load and display file.
See Picture.new_for_file for details.
::: warning
Note that this function should not be used with untrusted data.
Use a proper image loading framework such as libglycin, which can
load many image formats into a GdkTexture, and then use
Image.set_from_paintable.
Parameters:
file— aGFile
set_filename¶
def set_filename(self, filename: str | bytes | os.PathLike[str] | os.PathLike[bytes] | None = ...) -> None
Makes self load and display the given filename.
This is a utility function that calls Picture.set_file.
::: warning
Note that this function should not be used with untrusted data.
Use a proper image loading framework such as libglycin, which can
load many image formats into a GdkTexture, and then use
Image.set_from_paintable.
Parameters:
filename— the filename to play
set_isolate_contents¶
If set to true, then the contents will be rendered individually.
If set to false they will be able to erase or otherwise mix with the background.
GTK supports finer grained isolation, in rare cases where you need
this, you can use Snapshot.push_isolation yourself to
achieve this.
By default contents are isolated.
Parameters:
isolate_contents— if contents are rendered separately
set_keep_aspect_ratio¶
:::warning Deprecated since 4.8 This API is deprecated. :::
If set to True, the self will render its contents according to
their aspect ratio.
That means that empty space may show up at the top/bottom or
left/right of self.
If set to False or if the contents provide no aspect ratio,
the contents will be stretched over the picture's whole area.
Parameters:
keep_aspect_ratio— whether to keep aspect ratio
set_paintable¶
Makes self display the given paintable.
If paintable is NULL, nothing will be displayed.
See Picture.new_for_paintable for details.
Parameters:
paintable— aGdkPaintable
set_pixbuf¶
:::warning Deprecated since 4.12 This API is deprecated. :::
Sets a GtkPicture to show a GdkPixbuf.
See Picture.new_for_pixbuf for details.
This is a utility function that calls Picture.set_paintable.
Parameters:
pixbuf— aGdkPixbuf
set_resource¶
Makes self load and display the resource at the given
resource_path.
This is a utility function that calls Picture.set_file.
Parameters:
resource_path— the resource to set
Properties¶
alternative_text¶
The alternative textual description for the picture.
can_shrink¶
If the GtkPicture can be made smaller than the natural size of its contents.
content_fit¶
How the content should be resized to fit inside the GtkPicture.
file¶
The GFile that is displayed or None if none.
isolate_contents¶
If the rendering of the contents is isolated from the rest of the widget tree.
keep_aspect_ratio¶
:::warning Deprecated since 4.8 This API is deprecated. :::
Whether the GtkPicture will render its contents trying to preserve the aspect ratio.
paintable¶
The GdkPaintable to be displayed by this GtkPicture.