I have written a Class that extend Gtk.image to automatically fit the parent size based on PyGTK: How do I make an image automatically scale to fit it's parent widget? but with some improvements and corrections.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GdkPixbuf, GLib
class ImageEx(Gtk.Image):
pixbuf = None
def __init__(self, *args, **kwargs):
super(ImageEx, self).__init__(*args, **kwargs)
self.connect("size-allocate", self.on_size_allocate)
self.props = super(ImageEx, self).props
self.props.width_request = 100
self.props.height_request = -1
self.props.hexpand = True
self.tempHeight = 0
self.tempWidth = 0
self.origHeight = 0
self.origWidth = 0
def set_from_pixbuf(self, pixbuf):
self.pixbuf = pixbuf
self.origHeight = pixbuf.get_height()
self.origWidth = pixbuf.get_width()
self.tempHeight = 0
self.tempWidth = 0
self.on_size_allocate(None, self.get_allocation())
def calculateHeight(self, desiredWidth):
return desiredWidth * self.origHeight / self.origWidth
def on_size_allocate(self, obj, rect):
if self.pixbuf is not None:
desiredWidth = min(rect.width-10, self.origWidth)
if self.tempWidth != desiredWidth:
self.tempWidth = desiredWidth
self.tempHeight = self.calculateHeight(desiredWidth)
pixbuf = self.pixbuf.scale_simple(self.tempWidth,
self.tempHeight,
GdkPixbuf.InterpType.BILINEAR)
GLib.idle_add(super(ImageEx, self).set_from_pixbuf,
pixbuf)
I use it in a Gtk.Paned, it works correctly, I can resize the panel, and the window, but if I expand the window I can't restore it with the window button but if I move the window, it restores correctly.
How can a so inconsistent state exist? How can I correct it?
Thank you