from gi.repository import Gtk, GObject class Sizer(Gtk.Bin): """ A widget that lets you control the natural size like the size request """ natural_width = GObject.Property(type=int, default=0) natural_height = GObject.Property(type=int, default=0) def __init__(self, **kwargs): Gtk.Bin.__init__(self, **kwargs) def do_get_preferred_width(self) -> (int, int): minimum, natural = Gtk.Bin.do_get_preferred_width(self) if self.natural_width > 0: natural = max(minimum, self.natural_width) return (minimum, natural) def do_get_preferred_height(self) -> (int, int): minimum, natural = Gtk.Bin.do_get_preferred_height(self) if self.natural_height > 0: natural = max(minimum, self.natural_height) return (minimum, natural) def do_get_preferred_width_for_height(self, height: int) -> (int, int): minimum, natural = Gtk.Bin.do_get_preferred_width_for_height(self, height) if self.natural_width > 0: natural = max(minimum, self.natural_width) return (minimum, natural) def do_get_preferred_height_for_width(self, width: int) -> (int, int): minimum, natural = Gtk.Bin.do_get_preferred_height_for_width(self, width) if self.natural_height > 0: natural = max(minimum, self.natural_height) return (minimum, natural)