Image not expanding to fill available space

This worked fine with Glade/Gtk3+ but after conversion to PyGobject/GTK4 I can’t make it work. I have pixbuf images within boxes within boxes. The main image is supposed to expand to fill the space available, but doesn’t. A portrait image expands to fit the height available leaving the expected gaps on each side, but a landscape image only appears to expand until it is as wide as the available height - that may be coincidence. For testing I’ve simplified it to just one image inside the main window and it still doesn’t work.

  1. I use expand and fill on the window, boxes and images:
image1.props.hexpand = True	
image1.props.vexpand = True
image1.props.halign = Gtk.Align.FILL
image1.props.valign = Gtk.Align.FILL

Is that enough, if not what should I be using?

  1. I want to rotate the images easily where necessary so use:

pixbuf = GdkPixbuf.Pixbuf.rotate_simple({see below}, 90)

and have tried:

GdkPixbuf.Pixbuf.new_from_file_at_scale(filename, width, height, True)
GdkPixbuf.Pixbuf.new_from_file_at_size(filename, width, height, True)
GdkPixbuf.Pixbuf.new_from_file_at_size(filename)

then

 image1.set_from_pixbuf(pixbuf)

Should I be using something different?

I think I have tried all possible combinations available and have no more ideas!

The GtkImage widget in GTK3 was split into two separate widgets in GTK4:

  • GtkImage should be used for icon assets, in other words: content that you always want to display at a 1:1 scale and that defines the size of the widget
  • GtkPicture should be used for image assets: content that is defined by the size of the widget

If you’re displaying an image, you want to switch your GtkImage to a GtkPicture.

1 Like

Thanks, GtkPicture fills the space so I’ll switch to using that.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.